Visual Basic and Visual C ++ interaction (Standard DLL)

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.