Hi,
I am looking for a way to pass structs or a similar type of thing from a C++ DLL to VB.
Has anyone got any ideas.....
Regards
IJ...
Printable View
Hi,
I am looking for a way to pass structs or a similar type of thing from a C++ DLL to VB.
Has anyone got any ideas.....
Regards
IJ...
You could do it the way windows API does it:
C++:
VB:Code:struct my_struct
{
int a,b;
char c;
something* d;
};
void _stdcall my_function(my_struct& the_struct)
{
the_struct.a=1;
the_struct.b=2;
the_struct.c="3";
the_struct.d=&my_something;
}
VB Code:
Type my_struct 'The same as the other struct, but VB doesn't have pointers so use a Long instead Long a Long b Byte c Long d End Type Declare Sub my_function Lib "dll.dll" (ByRef Struct As my_struct) Sub MySub() Dim ReturnValue As my_struct my_function ReturnValue MsgBox ReturnValue.a End Sub
Thanks man, your a genius :D
But how do I get the string value, not too sure about that
Thanks
IJ...
A VB string passed ByVal can be used in C++ as a normal wchar_t:
You could also use C style string functions instead of the C++ ones. Also, make sure that the string is large enough, and that it is not a variant:Code:#include <string>
#include <algorithm>
void _stdcall takes_a_string(wchar_t* the_string)
{
std::wstring my_string = the_string;
the_string=L"hi" + L", bye";
std::copy(my_string.begin(),my_string.end(),the_string);
the_string[my_string.size()]=0;
}
Also note that although you are passing the string ByVal, you can still modify it in C++VB Code:
Declare Sub takes_a_string Lib "dll.dll" (ByVal aString as String) Sub MyOtherSub() Dim MyString As String MyString=String(100," ") ' Make sure it is large enough takes_a_string MyString ' Everything until the \0, which markes the end of a C style string MyString=Left$(MyString,InStr(MyString,vbNullChar)-1) MsgBox MyString End Sub
I don't know if it also works for a string in a struct...
Could pointers (in C) be used for something like this?
You mean, to pass strings? Maybe. Actually quite possible...
Why not try? Use VarPtr, or Copymemory!
The example creates shows how to
1. create a DLL in C/C++ which returns a structure
2. calls that DLL in VB (using VarPtr and CopyMemory)
[ccode]
#include<string.h>
#include<math.h>
#include<malloc.h>
struct quad
{
float r1;
float r2;
char* status;
};
quad* _stdcall solve_quad(int *a);
quad* _stdcall solve_quad(int *a)
{
quad *x;
x=(quad*)malloc(sizeof(quad));
int p,q,r;
float determ;
p=*(a+0);
q=*(a+1);
r=*(a+2);
determ=q*q-4*p*r;
if(determ<0) /* no real root */
{
x->r1=0;
x->r2=0;
x->status=(char*)malloc(13*sizeof(char));
strcpy(x->status,"No real root");
return x;
}
else /* real root exists */
{
x->r1=((-q)+sqrt(determ))/(2*p);
x->r2=((-q)-sqrt(determ))/(2*p);
x->status=(char*)malloc(15*sizeof(char));
strcpy(x->status,"Two real roots");
return x;
}
}
[/ccode]
Create also the .def file as usual.
LIBRARY ArrayStructure
EXPORTS solve_quad
Now use the following code to call the function in VB.
VB Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, ByVal Source As Any, ByVal Length As Long) Private Declare Function solve_quad Lib "arraystructure.dll" _ (ByVal m As Long) As Long Private Sub Command1_Click() Dim adr As Long Dim z(3) As Long Dim Root1 As Single Dim Root2 As Single Dim Solution As String z(0) = 1 z(1) = -5 z(2) = 6 adr = solve_quad(VarPtr(z(0))) Call CopyMemory(Root1, adr, 4) Call CopyMemory(Root2, adr + 4, 4) Call CopyMemory(Solution, adr + 8, 4) MsgBox "Root1 = " & Root1 & vbCrLf & "Root2 = " & Root2 _ & vbCrLf & "Solution = " & Solution End Sub
Execute the VB program and you’ ll see two roots and status of solution.