Sending and catching strings from a DLL
-
Hello, I have difficulties in using a DLL I was given. So I can't modify the code of this DLL, but I know that it works, since I tested it succesfully with a C program (with the .h and .lib files that were given with it). My problem is I have to call it in VB .NET. Here is the function that I have to call in the DLL:
void myfunction( long * param_one, long param_two, unsigned char * param_three, unsigned char * param_four)
Parameters 1 and 4 are outputs and parameters 2 and 3 are inputs. And here is how I declare and call it in my VB Code:Private Declare Sub myfunction Lib "mydll" _ (ByRef param_one As Long, _ ByVal param_two As Long, _ ByVal param_three As String, _ ByVal param_four As String)
Call myfunction(a,b,c,d)
It seems the string input (param_three) doesn't reach the DLL, which I know from the return code (param_one). I've tested with ByRefs for the strings but it doesn't work either. Maybe is it a problem with the fact that these are unsigned char * ? I am a brand new DLL user so I may have made mistakes... If someone has an idea, I would really appreciate it. -
Hello, I have difficulties in using a DLL I was given. So I can't modify the code of this DLL, but I know that it works, since I tested it succesfully with a C program (with the .h and .lib files that were given with it). My problem is I have to call it in VB .NET. Here is the function that I have to call in the DLL:
void myfunction( long * param_one, long param_two, unsigned char * param_three, unsigned char * param_four)
Parameters 1 and 4 are outputs and parameters 2 and 3 are inputs. And here is how I declare and call it in my VB Code:Private Declare Sub myfunction Lib "mydll" _ (ByRef param_one As Long, _ ByVal param_two As Long, _ ByVal param_three As String, _ ByVal param_four As String)
Call myfunction(a,b,c,d)
It seems the string input (param_three) doesn't reach the DLL, which I know from the return code (param_one). I've tested with ByRefs for the strings but it doesn't work either. Maybe is it a problem with the fact that these are unsigned char * ? I am a brand new DLL user so I may have made mistakes... If someone has an idea, I would really appreciate it.It depends on what the .DLL is doing with the string. If it's creating a string for return, then what you did should work. If it's expecting a string BUFFER passed in (which is what this looks like), you have to pass in a buffer for a string and not just a pointer to en empty string. The buffered version can be done with a StringBuilder instead of a String.
Private Declare Sub myFunction Lib "mydll" ( _
ByRef param_one As Long, _
ByVal param_two As Long, _
ByVal param_three As StringBuilder, _
...)
' To call the thing, create the buffer first.
Dim buffer As new StringBuilder( 512 )
myFunction( a, b, buffer, d )You can get more information and techniques from here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Hello, I have difficulties in using a DLL I was given. So I can't modify the code of this DLL, but I know that it works, since I tested it succesfully with a C program (with the .h and .lib files that were given with it). My problem is I have to call it in VB .NET. Here is the function that I have to call in the DLL:
void myfunction( long * param_one, long param_two, unsigned char * param_three, unsigned char * param_four)
Parameters 1 and 4 are outputs and parameters 2 and 3 are inputs. And here is how I declare and call it in my VB Code:Private Declare Sub myfunction Lib "mydll" _ (ByRef param_one As Long, _ ByVal param_two As Long, _ ByVal param_three As String, _ ByVal param_four As String)
Call myfunction(a,b,c,d)
It seems the string input (param_three) doesn't reach the DLL, which I know from the return code (param_one). I've tested with ByRefs for the strings but it doesn't work either. Maybe is it a problem with the fact that these are unsigned char * ? I am a brand new DLL user so I may have made mistakes... If someone has an idea, I would really appreciate it.For all I know the type of parameters three and four (string) should be LPSTR. void myfunction(long * param_one, long param_two,LPSTR * param_three,LPSTR * param_four) . . . we have same problem encountered when i create a dll in VC++, and it works when I used LPSTR, u can try this. I hope it helps
-
It depends on what the .DLL is doing with the string. If it's creating a string for return, then what you did should work. If it's expecting a string BUFFER passed in (which is what this looks like), you have to pass in a buffer for a string and not just a pointer to en empty string. The buffered version can be done with a StringBuilder instead of a String.
Private Declare Sub myFunction Lib "mydll" ( _
ByRef param_one As Long, _
ByVal param_two As Long, _
ByVal param_three As StringBuilder, _
...)
' To call the thing, create the buffer first.
Dim buffer As new StringBuilder( 512 )
myFunction( a, b, buffer, d )You can get more information and techniques from here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Thanks for your help. Although I'm now using a StringBuilder, the parameter doesn't seem to get in the DLL... I really wonder why this doesn't work :(
-
For all I know the type of parameters three and four (string) should be LPSTR. void myfunction(long * param_one, long param_two,LPSTR * param_three,LPSTR * param_four) . . . we have same problem encountered when i create a dll in VC++, and it works when I used LPSTR, u can try this. I hope it helps
Yes, I've already read that somewhere, but the problem is I can't modify the DLL... Thanks anyway.