|
-
Oct 31st, 2002, 09:00 AM
#1
Thread Starter
Lively Member
passing structs
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...
-
Oct 31st, 2002, 09:38 AM
#2
You could do it the way windows API does it:
C++:
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:
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
-
Oct 31st, 2002, 09:41 AM
#3
Thread Starter
Lively Member
Thanks man, your a genius 
But how do I get the string value, not too sure about that
Thanks
IJ...
-
Oct 31st, 2002, 03:07 PM
#4
A VB string passed ByVal can be used in C++ as a normal wchar_t:
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;
}
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:
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
Also note that although you are passing the string ByVal, you can still modify it in C++
I don't know if it also works for a string in a struct...
-
Oct 31st, 2002, 06:01 PM
#5
Junior Member
Could pointers (in C) be used for something like this?
-
Oct 31st, 2002, 06:13 PM
#6
Good Ol' Platypus
You mean, to pass strings? Maybe. Actually quite possible...
Why not try? Use VarPtr, or Copymemory!
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 5th, 2002, 07:23 AM
#7
Fanatic Member
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.
Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|