I'm trying to figure out how to pass a user defined structure from a VB6 application to a C++ DLL.
Here's a sample of my VB6 code :
Private Type ObjetVB
Rank As Integer
Id As String
End Type
Private Declare Sub testLObj Lib "D:\TestDLL.dll" (Tab_Obj() As ObjetVB)
Private Declare Sub testObj Lib "D:\TestDLL.dll" (ByRef Obj As ObjetVB)
Private Sub Command1_Click()
Dim elements(1 To 4) As ObjetVB, i As Long
For i = 1 To 4
elements(i).Rank = i
elements(i).Id = "Pouet"
Next
testLObj elements()
End Sub
Private Sub Command2_Click()
Dim ObjCrash As ObjetVB
ObjCrash.Rank = 1
ObjCrash.Id = "Pouet"
testObj ObjCrash
End Sub
And a sample of my C++ code :
struct ObjetVB
{
short Rank;
char* Id;
};
void videFichier()
{
ofstream fichier("../../../log.txt", ios::out | ios::trunc);
if(fichier)
{
fichier.close();
}
}
int Log(ObjetVB ObjInput)
{
ofstream fichier("../../../log.txt", ios::out | ios::app);
if(fichier)
{
fichier << ObjInput.Rank << endl << "Id : " << ObjInput.Id << endl << endl;
fichier.close();
}
return 0;
}
void __stdcall testObj (ObjetVB* ObjInput)
{
videFichier();
log(*ObjInput);
}
void __stdcall testLObj (SAFEARRAY **Tab_Obj)
{
ObjetVB *elt;
HRESULT ret;
unsigned long i;
videFichier();
if ((ret = SafeArrayAccessData(*Tab_Obj,(void **) &elt))==S_OK)
{
for (i = 0; i < (*Tab_Obj)->rgsabound->cElements; i++)
{
Log(elt[i]);
}
SafeArrayUnaccessData(*Tab_Obj);
}
}
My Issue is, when I click "Command2", my log file looks like this :
1
Id : Pouet
Whereas, when I click "Command1", it looks like this :
1
Id : P
2
Id : P
3
Id : P
4
Id : P
Why does my C++ DLL recognize "char* Id" as a chain of characters when I pass a single item whereas when I use an array of items it looks like it recognizes it as a pointer to the first character?
And, most notably, how could I fix it? I tried using LBSTR instead of char* in my c++ struct, it didn't fix it, I also tried to add "elements(i).Id = String (255, vbNullChar)" berfore initializing the VB6 strings, but it didn't prove to be helpful either.
As usual, I'd like to thank all of you for the time you put into reading and trying to help.
On a side note : English is a foreign langage to me, so I hope I'm almost understandable and, of course, I apologize if it's not the case.
Edit :
I don't know if it could help, but after trying what Mark Bertenshaw suggested, I also tried this : in VB6 :
Id As String * 10
in C++ :
char Id[10];
Which gave this weird result :
Objet :
32
Id : P
Objet :
1
Id :
Objet :
32
Id : P
Objet :
2
Id :