For more information, see Nicholas skapura's interfacing VB and C ++
VC ++ DLL
1) function declaration in DLL
Void _ declspec (dllexport) callback testfunc ()
{
Cout <"inside the DLL! ";
}
2) define the def File
Library "testdll_library"
Description "An example DLL for interfacing with C ++"
Exports
Testfunc @ 1
The VC ++ client calls testfunc'sCode
1) declare first
Typedef int (winapi * ifunc) (INT t );
Ifunc retint;
2) obtain the function pointer through loadlibrary and getprocaddress.
Hinstance hlib = loadlibrary ("testdll_library.dll ");
If (hlib = NULL)
{
Cout <"error: Unable to load library! "<Endl;
Getch ();
Return;
}
Retint = (ifunc) getprocaddress (hmodule) hlib, "retint ");
3) Call the functions in the DLL.
If (retint! = NULL)
Retint ();
Else
...
4) Release DLL
Freelibrary (hmodule) hlib );
Code used by the VB client to call testfunc
1) Statement
Declare function retint lib "../testdll_library.dll" (byval T as integer) as integer
2) Call
Call retint (1)
For more information, see <a href = "http://www.flipcode.com/articles/article_vbdlls.shtml">Interfacing Visual Basic and C ++</A>
Others
1) If VB needs to pass array parameters. Then vc dll needs to be completed in the following way
Short _ declspec (dllexport) callback arrayexample (lpsafearray far * arraydata)
{
Short * temp;
Temp = (short *) (* arraydata)-> pvdata;
Return (temp [0]);
}
2) The parameter contains a string
BSTR _ declspec (dllexport) callback stringexample (BSTR stringvar)
{
Lpstr buffer;
Buffer = (lpstr) stringvar;
: MessageBox (null, buffer, "in C ++", 0 );
Buffer = _ strrev (buffer );
Return (sysallocstring (stringvar ));
}
3) returns a string. You must call sysallocstring to allocate space to the string in advance.
Return (sysallocstring (temparray [Index]);
4) callback function
// VC ++ DLL
// In C ++
Void _ declspec (dllexport) callback callbackexample (long addr1)
{
Typedef void (_ stdcall * fnptr) (BSTR stringvar );
Fnptr functioncall;
Lpstr buffer = "Hello! ";
Functioncall = (fnptr) addr1;
BSTR temp;
Temp = chartobstr ("hello ");
Functioncall (sysallocstring (temp ));
}
VB Client
Declare sub callbackexample lib "../testdll_library.dll" (byval ADDR as long)
Public sub voidfunc (byval stringvar as string)
Msgbox stringvar
End sub