There are two methods to declare the export function in dll:
One way is to add _ declspec (dllexport) in the function declaration );
Another method is to use the module definition (. Def) file declaration, and the (. Def) file provides the link for the linkerProgram.
Method 1: Add _ declspec (dllexport) to the function declaration)
/// In the Dynamic Linked Library Program
/// Declare the external interface function testfuction of the dynamic link library (**. dll)
Extern "C" _ declspec (dllexport) int testfuction (INT ntype, char * strpath, STD: vector <string> & vecdata)
{
/// Do anything here ////
Return 0;
}
/// In the program that calls the dynamic link library externally
/// Load the dynamic link library (**. dll) and call its external interface testfuction
Void func ()
{
// The function pointer of the same type as the testfuction function is testdll.
Typedef int (_ cdecl * testdll) (INT ntype, char * strpath, STD: vector <string> & vecdata );
Hinstance hmod;
// Load the dynamic link library **. dll
Hmod =: loadlibrary (_ text ("DLL relative path \ *. dll "));
If (null = hmod)
{
Trace ("loading **. dll failed ");
}
// Define a function pointer of the same type as the function testfuction lpproc
Testdll lpproc;
// Search for the external interface named testfuction in **. dll
Lpproc = (testdll) getprocaddress (hmod, "testfuction ");
// If the search is successful
If (null! = Lpproc)
{
Int ntype = 0;
Char * strpath = "data ";
STD: vector <string> vecdata;
// Call the **. DLL interface function testfuction through the function pointer lpproc
Int nresult = (* lpproc) (ntype, strpath, vecdata );
}
//...
// Release the dynamic link library **. dll when appropriate
Freelibrary (hmod );
}
Method 2: Use the module definition (. Def) file Declaration
First create a DLL Program (dlltestdef)
In *. cpp
Int _ stdcall add (int numa, int numb)
{
Return (NUMA + numb );
}
Int _ stdcall sub (int numa, int numb)
{
Return (NUMA-Numb );
}
Create a. Def file and add
; Dlltestdef. Lib: export the DLL Function
; Author :----
Library dlltestdef
Exports
Add @ 1
Sub @ 2
Finally, create a test program:. cpp file as follows:
# Include <iostream>
# Include <windows. h>
Using namespace STD;
Typedef int (_ stdcall * Fun) (INT, INT );
Hinstance;
Fun Fun;
Int main ()
{
Hinstance = loadlibrary ("dlltestdef. dll ");
If (! Hinstance)
Cout <"not find this DLL" <Endl;
Fun = (fun) getprocaddress (hinstance, makeintresource (1 ));
If (! Fun)
{
Cout <"not find this fun" <Endl;
}
Cout <fun (1, 2) <Endl;
Freelibrary (hinstance );
Return 0;
}
Note:
The. Def file rules are as follows:
(1) The Library statement describes the DLL corresponding to the. Def file;
(2) Name of the function to be exported after the exports statement. You can add @ n after the export function name in the. Def file to indicate that the sequence number of the function to be exported is n (this sequence number will play its role during function calling );
(3) The annotation in the def file is specified by the semicolon (;) at the beginning of each comment line, and the comment cannot share a line with the statement.
(4) using _ declspec (dllexport) is different from using a. Def file.
If your DLL is provided to VC users, you only need to provide the. Lib generated during DLL compilation to users,
It can easily call your DLL. However, if your DLL is used by VB, Pb, and Delphi users, it will cause a small headache.
The VC ++ compiler converts the name of the _ declspec (dllexport) declared function, as shown in the following code:
_ Declspec (dllexport) int _ stdcall add ()
Will be converted to add @ 0, so that you must declare in VB as follows:
Declare function add lib "dlltestdef. dll" alias "add @ 0" () as long
@ The following number may vary depending on the parameter type. This is obviously inconvenient. To avoid this conversion, use the. Def file to export the function.