extra spaces in string when returned from a dll
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: extra spaces in string when returned from a dll

  1. #1
    Join Date
    Sep 2005
    Posts
    13

    extra spaces in string when returned from a dll

    I have a dll that takes a string and character length. It is supposed to returned the string justified based on the character length (fixed field sizes). However, when the string returns from the dll its length is twice that specified by the character length variable passed to it. If anyone could provide some insight to this phenomenon I would appreciate it.

  2. #2
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: extra spaces in string when returned from a dll

    If I had to guess i would say there is some kind of issue between converting this string between ACSII and UNICODE (wide character) formats.
    Mike

  3. #3
    Join Date
    Sep 2005
    Posts
    13

    Re: extra spaces in string when returned from a dll

    Thanks Pinky98.

    Is there any work around for this tha I can implement?

  4. #4
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: extra spaces in string when returned from a dll

    How are you calling the DLL? Is it using the "Private Declare" method. Try to change the parameter type. Instead of using "Byval var As String", make it a "Byref var as Any" and pass a pointer to a byte array to it.

    This is the original code (for example)
    Code:
    Private Declare Function GetWindowsDirectoryW Lib "kernel32" ( _
       ByVal lpBuffer As String, ByVal nSize As String) As Long
    
    Private Sub Form_Load()
       Dim strText As String
       strText = Space(255)
       Call GetWindowsDirectoryW(strText, 255)
       MsgBox strText
    End Sub
    It will give the result same as yours (as I can understand ur problem). Make the changes like this
    Code:
    Private Declare Function GetWindowsDirectoryW Lib "kernel32" ( _
       ByRef lpBuffer As Any, ByVal nSize As String) As Long
    
    Private Sub Form_Load()
       Dim strText As String, bArrStr() As Byte
       bArrStr = Space(255)
       Call GetWindowsDirectoryW(bArrStr(LBound(bArrStr)), 255)
       strText = bArrStr
       MsgBox strText
    End Sub
    Hope it will help you

  5. #5
    Join Date
    Sep 2005
    Posts
    13

    Re: extra spaces in string when returned from a dll

    no luck rxbagain

  6. #6
    Join Date
    Apr 2003
    Posts
    1,755

    Re: extra spaces in string when returned from a dll

    How is the function declared and how are you calling it? Can you post your declaration and the code that calls it here?

  7. #7
    Join Date
    Sep 2005
    Posts
    13

    Re: extra spaces in string when returned from a dll

    mind you, this is a rather simple test code

    Private Declare Function FSR Lib _
    "C:\MyDllLibrary\BDF Formatting\BDFFormatting.dll" _
    (ByRef FSString As Any, ByRef StrinLen As Integer) As String
    Private Declare Function FSL Lib _
    "C:\MyDllLibrary\BDF Formatting\BDFFormatting.dll" _
    (ByRef FSString As Any, ByRef StrinLen As Integer) As String

    Sub Command1_Click()
    Dim FName As String: FName = "C:\test\bdfformat.txt"
    Dim FNum As Integer: FNum = FreeFile
    Dim tstr As String: tstr = Trim$(Form1.Text1)
    Dim strlen As Integer: strlen = Int(Trim$(Form1.Text2))
    Dim bArrStr() As Byte: bArrStr = Space(strlen)

    Screen.MousePointer = vbHourglass
    Form1.Text3.Text = Len(FSR(tstr, strlen))
    Form1.Text4.Text = Len(FSL(tstr, strlen))
    Form1.Text5.Text = Len(tFSR(tstr, strlen))
    Form1.Text6.Text = Len(tFSL(tstr, strlen))
    Open FName For Output As FNum
    Print #FNum, FSR(tstr, strlen)
    Print #FNum, FSL(tstr, strlen)
    Print #FNum, tFSR(tstr, strlen)
    Print #FNum, tFSL(tstr, strlen)
    Close FNum
    Screen.MousePointer = vbDefault

    End Sub

    The code take a string and an integer value from the form, passes them to the dll to either right or left justify the string based on the integer value.

    Thanks for you help

  8. #8
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: extra spaces in string when returned from a dll

    Convert the return value from Unicode. This is because your DLL returns BSTR but VB treats all strings (when calling dll function usiing Declare) as single byte which caused the addition of byte 0s in between.
    Code:
    Form1.Text3.Text = Len(StrConv(FSR(tstr, strlen), vbFromUnicode))
    Form1.Text4.Text = Len(StrConv(FSL(tstr, strlen), vbFromUnicode))
    Form1.Text5.Text = Len(tFSR(tstr, strlen))
    Form1.Text6.Text = Len(tFSL(tstr, strlen))
    Open FName For Output As FNum
    Print #FNum, StrConv(FSR(tstr, strlen), vbFromUnicode)
    Print #FNum, StrConv(FSL(tstr, strlen), vbFromUnicode)
    Print #FNum, tFSR(tstr, strlen)
    Print #FNum, tFSL(tstr, strlen)
    Close FNum
    Check also the parameter that you pass (FSString). If the function expects ANSI string, use the Byval. If it is unicode, use the Byref As Any. But I think, the original one (Byval as String) u used before is fine because it was working before except the return is not ok.

    Hope it will work for you

  9. #9
    Join Date
    Sep 2005
    Posts
    13

    Re: extra spaces in string when returned from a dll

    ok, this works when for truncating the length, but now I no longer get the string returned. Instead, I get ?? in place of the string.

  10. #10
    Join Date
    Apr 2003
    Posts
    1,755

    Re: extra spaces in string when returned from a dll

    Have you returned the original function declarations?
    Code:
    Private Declare Function FSR Lib _
    "C:\MyDllLibrary\BDF Formatting\BDFFormatting.dll" _
    (ByVal FSString As String, ByRef StrinLen As Integer) As String
    Private Declare Function FSL Lib _
    "C:\MyDllLibrary\BDF Formatting\BDFFormatting.dll" _
    (ByVal FSString As String, ByRef StrinLen As Integer) As String
    By the way, what are tFSR and tFSL functions?

  11. #11
    Join Date
    Sep 2005
    Posts
    13

    Re: extra spaces in string when returned from a dll

    yes to the first question

    tFSR and tFSL are the same functions in the dll. Just making a check for this example. Here they are.

    'this function right justifies FSString into a field StrinLen long.
    Public Function tFSR(FSString As String, StrinLen As Integer) As String
    tFSR = Format$(Right$((FSString), StrinLen), String(StrinLen, "@"))
    End Function

    'this function left justifies FSString into a field StrinLen long.
    Public Function tFSL(FSString As String, StrinLen As Integer) As String
    tFSL = Format$(Left$((FSString), StrinLen), "!" & String(StrinLen, "@"))
    End Function

  12. #12
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: extra spaces in string when returned from a dll

    Hmmm.... I think the DLL function requires BSTR too. Try to convert the the string to unicode before passing to the function
    Code:
    Form1.Text3.Text = Len(StrConv(FSR(StrConv(tstr, vbUnicode), strlen), vbFromUnicode))
    Form1.Text4.Text = Len(StrConv(FSL(StrConv(tstr, vbUnicode), strlen), vbFromUnicode))
    Form1.Text5.Text = Len(tFSR(tstr, strlen))
    Form1.Text6.Text = Len(tFSL(tstr, strlen))
    Open FName For Output As FNum
    Print #FNum, StrConv(FSR(StrConv(tstr, vbUnicode), strlen), vbFromUnicode)
    Print #FNum, StrConv(FSL(StrConv(tstr, vbUnicode), strlen), vbFromUnicode)
    Print #FNum, tFSR(tstr, strlen)
    Print #FNum, tFSL(tstr, strlen)
    Close FNum
    If this won't work, pls post the final code that you have together with the function declaration you have.

  13. #13
    Join Date
    Sep 2005
    Posts
    13

    Re: extra spaces in string when returned from a dll

    rxbagain,

    Sorry for the long delay in replying, I have been out of the office for the last week. converiting to unicode before passing to the dll didn't work. I added a line in the code to see what is coming back from the dll. I believe the problem is in the StrConv function (but not exactly sure). Here is the code that I have at this point (the 2 functions at the end are the same as the dll functions)

    Private Declare Function FSR Lib _
    "C:\MyDllLibrary\BDF Formatting\BDFFormatting.dll" _
    (ByVal FSString As String, ByRef StrinLen As Integer) As String
    Private Declare Function FSL Lib _
    "C:\MyDllLibrary\BDF Formatting\BDFFormatting.dll" _
    (ByVal FSString As String, ByRef StrinLen As Integer) As String

    Sub Command1_Click()
    Dim FName As String: FName = "C:\test\bdfformat.txt"
    Dim FNum As Integer: FNum = FreeFile
    Dim tstr As String: tstr = Trim$(Form1.Text1)
    Dim strlen As Integer: strlen = Int(Trim$(Form1.Text2))

    Screen.MousePointer = vbHourglass
    tmptxt = FSR(tstr, strlen)
    tmptxt = StrConv(tmptxt, vbFromUnicode)
    Form1.Text3.Text = StrConv(FSR(tstr, strlen), vbFromUnicode)
    Form1.Text4.Text = StrConv(FSL(tstr, strlen), vbFromUnicode)
    Form1.Text5.Text = tFSR(tstr, strlen)
    Form1.Text6.Text = tFSL(tstr, strlen)
    'Open FName For Output As FNum
    'Print #FNum, StrConv(FSR(tstr, strlen), vbFromUnicode)
    'Print #FNum, StrConv(FSL(tstr, strlen), vbFromUnicode)
    'Print #FNum, tFSR(tstr, strlen)
    'Print #FNum, tFSL(tstr, strlen)
    'Close FNum
    Screen.MousePointer = vbDefault

    End Sub

    'this function right justifies FSString into a field StrinLen long.
    Public Function tFSR(FSString As String, StrinLen As Integer) As String
    tFSR = Format$(Right$((FSString), StrinLen), String(StrinLen, "@"))
    End Function

    'this function left justifies FSString into a field StrinLen long.
    Public Function tFSL(FSString As String, StrinLen As Integer) As String
    tFSL = Format$(Left$((FSString), StrinLen), "!" & String(StrinLen, "@"))
    End Function

    Here is what is in the dll code

    Public Const DLL_PROCESS_DETACH = 0
    Public Const DLL_PROCESS_ATTACH = 1
    Public Const DLL_THREAD_ATTACH = 2
    Public Const DLL_THREAD_DETACH = 3

    Public Function DllMain(hInst As Long, fdwReason As Long, _
    lpvReserved As Long) As Boolean

    Select Case fdwReason
    Case DLL_PROCESS_DETACH
    'No per-process cleanup needed
    Case DLL_PROCESS_ATTACH
    DllMain = True
    Case DLL_THREAD_ATTACH
    'No per-thread initialization needed
    Case DLL_THREAD_DETACH
    'No per-thread cleanup needed
    End Select
    End Function

    'this function right justifies FSString into a field StrinLen long.
    Public Function FSR(ByVal FSString As String, ByRef StrinLen As Integer) As String
    FSR = Format$(Right$((FSString), StrinLen), String(StrinLen, "@"))
    End Function

    'this function left justifies FSString into a field StrinLen long.
    Public Function FSL(ByVal FSString As String, ByRef StrinLen As Integer) As String
    FSL = Format$(Left$((FSString), StrinLen), "!" & String(StrinLen, "@"))
    End Function

    Thanks for the help.

  14. #14
    Join Date
    Sep 2005
    Posts
    13

    Smile Re: extra spaces in string when returned from a dll

    rxbagain,

    Must have had something wrong when I tried your last suggestion of converting the string to unicode before passing to the dll. I tried it again and it seems to be working fine now. Here is the current line I send to the dll

    Form1.Text3.Text = StrConv(FSR(StrConv(tstr, vbUnicode), strlen), vbFromUnicode)
    Form1.Text4.Text = StrConv(FSL(StrConv(tstr, vbUnicode), strlen), vbFromUnicode)

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center