MFC
1. Create a DLL
1. Create an MFC dynamic link library. In section 4th, select the Regular DLL With MFC shared linked type, that is, the conventional DLL for creating a dynamic link With MFC, it can be used by Win32 and MFC applications.
2. Add a function. Add the following statement to the test. h file of the generated project:
Extern "C" void _ declspec (dllexport) print ();
The declaration statement of the function is added above, and then the function body is written in the. cpp file.
Extern "C" void _ declspec (dllexport) print ()
{
CString str = "DLL ";
AfxMessageBox (str );
}
The above code is added to the statement:
CTestApp theApp.
3. Compile and connect. Generate the DLL and LIB files in the debug folder of the current file.
Ii. Use DLL
1. Create a dialog box-based application. Add a button.
2. Click the response function of the button:
Hdll = LoadLibrary (".. \ Debug \ MFCDll. dll"); // import the dynamic link library
If (hdll)
{
Typedef void (* PROCTYPE )();
PROCTYPE myprint = (PROCTYPE) GetProcAddress (hdll, "print"); // obtain the function address
(* Myprint )();
}
FreeLibrary (hdll); // release the database
3. Copy the generated Dll file to the specified directory.
Win32
1. Create Win32 DLL
1. Create an empty Win32 dynamic link library project;
2. Add the C ++ source file.
3. Compile the function fun (int a, int B) in the code area );
Int fun (int a, int B)
{
Int num1 =;
Int num2 = B;
Int temp = 0;
If (a <B) {temp = a; a = B; B = temp ;}
While (B! = 0)/* use the rolling division until B is 0 */
{
Temp = a % B;
A = B;
B = temp;
}
Return num1 * num2/;
}
4. Add a header file.
Int fun (int a, int B );
5. Add the module definition file (. def ). Create a notepad file and change the suffix to. def.
The content is as follows:
; Test. def: Declares the module parameters for the DLL.
LIBRARY "Win32Dll"
DESCRIPTION 'test Windows Dynamic Link library'
EXPORTS
Fun; Explicit exports can go here
6. Add module files to the project. FileView panel --> right-click test files --> Add File ToProject...
7. Compile and connect.
Ii. Use Win32 DLL
1. Copy the generated DLL files, Lib files, and test. H files to the specified directory. And add: # include "test. h" to the. cpp file of the application"
2. implicitly load the Win32 Dll file. Select Project | Setting command, select the Link tag in the pop-up dialog box, and enter the file name of the Library file lib to be imported in the "Object/Library module" text box. Note the lib path.
Dynamic Link Library Loading
1. Implicit join
(1) "Project" | "Settings" command. The Project Setting dialog box is displayed.
(2) Link --> Objects/Library modules: add the Library template path.
(3) Place the DLL in the corresponding path.
2. Display connection:
(1) Call LoadLibrary ();
(2) Call GetProAddress ();
(3) Call FreeLibrary ();
Export data and functions in the dynamic link library
1. Create a module definition file: *. def
The content is as follows:
LIBRARY filename
DESCRIPTION description content
EXPORTS
Functionname
2. Use the keyword _ declspec (dllexport) to export members of functions, variables, classes, and classes.
For example, use _ declspec (dllexport) to export functions in the dynamic link library.
Int _ declspec (dllexport) _ cdecl functionname (int a, int B)
_ Declspec (dllexport) Export class member:
Class _ declspec (dllexport) MyCLASS
{
}