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.
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
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)
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.
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.
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.
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
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
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))
'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
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
Bookmarks