There are two methods to declare the export function in dll:
One is to add_ Declspec (dllexport ),Examples are not provided here;
Another method is to use the module definition (. Def) file declaration, and the. Def file provides the link for the linkerProgram.
First create a DLL program, 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 and 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.