This article describes how to use the def file to create a DLL.
The module definition (. Def) file is a text file that contains one or more module statements describing various DLL attributes. If you do not use_ Declspec (dllexport)If the keyword is used to export the DLL function, the DLL needs the. Def file .. The def file must contain at least the following module definition statements:
The first statement in the file must be a library statement. This statement identifies the. Def file as a DLL. The library statement is followed by the DLL name. The linker puts this name into the DLL Import and Export Database.
The exports statement lists the names and, if possible, the serial number values of the DLL export function. Assign the sequence number to the function by adding the @ sign and a number after the function name. When the sequence number value is specified, the sequence number range must be from 1 to n, where N is the number of DLL export functions. When using symbols, the external caller can use the function name and serial number to load the function.
Steps:
1. Create a Win32 console project, file-> New-> Project, select a Win32 console project, and select "DLL" and "Empty Project" in "application design"
2. Add a. cpp file to the project. This article is dlldeftest. cpp and enter the following code:
Int _ stdcall add (int numa, int numb)
{
Return (NUMA + numb );
}
Int _ stdcall sub (int numa, int numb)
{
Return (NUMA-Numb );
}
3. Add a. Def file to the project. This article is dlldeftest. Def. Enter the following code:
Library dlldeftest
Exports
Add @ 1
Sub @ 2
It can also be the following content, that is, the function name does not include the serial number.
Library dlldeftest
Exports
Add
Sub
The differences between the two are only different when the external import function is used.
4. compile the project and you can see dlldeftest. lib and dlldeftest. dll in debug.
Now that the DLL has been created, a test program is provided below to see how to use the DLL.
Create a Win32 console project, select "console application" and "Empty Project" in "Application Design", and add test to the project. CPP file, and enter the following code: The test program provided in this article calls the DLL by displaying the link. Of course, you can also call the DLL by implicit link, if implicit links are used, header files are required.
# Include <iostream>
# Include <windows. h>
Using namespace STD;
Typedef int (_ stdcall * Fun) (INT, INT );
Hinstance;
Fun and fun;
Int main ()
{
Hinstance = loadlibrary ("dlldeftest. dll ");
If (! Hinstance)
Cout <"not find this DLL" <Endl;
Fun = (fun) getprocaddress (hinstance, "add ");
// When the function serial number is specified in the def file, it can be exported using the serial number. Otherwise, it can only be exported using the function name.
// Fun = (fun) getprocaddress (hinstance, makeintresource (2 ));
If (! Fun)
{
Cout <"not find this fun" <Endl;
}
Cout <fun (1, 2) <Endl;
Freelibrary (hinstance );
Return 0;
}
Note: copy the generated dlldeftest. dll file to the test project directory.