How to get String from Pointer in VB6

1.7k views Asked by At

I must call C DLL with VB6.

C Code

short int decode(BOOL Mode, char* tete, char* adresse, char* status, char* nombre, char* datadecode);

My VB Code :

Private Declare Function decode Lib "VBdecode.dll" ( _ 
ByVal Mode As Long, _ 
ByVal tete As String, _ 
ByVal adresse As String, _ 
ByVal status As String, _ 
ByVal nombre As String, _ 
ByVal datadecode As String) As Long
Dim retour_lire As Long
Dim buffer(4) As Byte   
Dim  vbcData as string
Dim i As Integer
Dim chdecode As string

retour_lire = Byte_read(True, "4", "00", buffer, "16", vbcData)

For i = 1 To 10 
chdecode = vbcData(i) 
Next

MsgBox chdecode

but my VB6 code is not functional.

please any ideas, any proposals or corrections.

please help me, I count on you.

1

There are 1 answers

2
Mike Weir On

The comments are actually wrong. You are right. VB6 will convert Strings (sent that way, anyway) as char*. If you were using VarPtr() or using As Any, they would be right.

Your problem is the return value. Either change short int on the C++ side to int or change As Long on the VB6 side to As Integer.

So either:

int decode(BOOL Mode, char* tete, char* adresse, char* status, char* nombre, char* datadecode);

or:

Private Declare Function decode Lib "VBdecode.dll" ( _ 
ByVal Mode As Long, _ 
ByVal tete As String, _ 
ByVal adresse As String, _ 
ByVal status As String, _ 
ByVal nombre As String, _ 
ByVal datadecode As String) As Integer

Not both :)!

It's worth noting, your sample code doesn't ever actually call decode(). So another issue, and this is just a guess, is that you're trying to change the contents of a string on the C++ side. You can make changes to the string, but you can't reallocate it. So you need to have it already sized (using Space$()).