The DLL application in C ++ is a very important basic concept in actual programming. mastering this application plays an important role in program development. Here we will give a detailed introduction to C ++ conventional DLL, hoping to help you.
C ++ conventional DLL creation and use
1: export functions
- __declspec(dllexport) void ShowDlg(void)
- {
- CDllDialog dllDialog;
- dllDialog.DoModal ();
- }
2: Modify the. def file and add the function name.
- ; DrawTestDll.def : Declares the module parameters for the DLL.
-
- LIBRARY "DrawTestDll"
- DESCRIPTION 'DrawTestDll Windows Dynamic Link Library'
-
- EXPORTS
- ; Explicit exports can go here
3: copy the DLL file to the directory where the program is located.
4: Call the DLL in the program
- Void CDllTestView: OnLButtonDown (UINT nFlags, CPoint point)
- {
- Typedef void (* lpFun) (void );
- HINSTANCE hDll; // DLL handle
- HDll = LoadLibrary ("DrawTestDll. dll"); // load DLL Resources
- If (NULL = hDll) // search for the sequence, directory where EXE is located, directory where the project is located, and directory of System and windows
- {
- MessageBox ("DLL loading failed ");
- }
- LpFun addFun; // function pointer
- AddFun = (lpFun) GetProcAddress (hDll, "ShowDlg"); // obtain the address of the ShowDlg function.
- If (NULL = addFun)
- {
- MessageBox ("failed to search for functions in DLL ");
- }
- HINSTANCE exe_hinstance = AfxGetResourceHandle (); // obtain the instance handle of the main program
- AfxSetResourceHandle (hDll); // set the main program handle to a DLL instance to use resources in the DLL)
- AddFun (); // call a function
- AfxSetResourceHandle (exe_hinstance); // reset the instance handle of the main program
- FreeLibrary (hDll); // release DLL Resources
- }
C ++ common DLL class usage
1: Add a new class to the DLL
Class. h file:
- // Export a class (including its methods and attributes)
- Class _ declspec (dllexport) CDllClass {
- Public:
- CDllClass (void );
- Void MSG (const char * const str );
- };
Class. cpp file:
- #include "stdafx.h"
- #include "DllClass.h"
-
- CDllClass::CDllClass()
- {
- return;
- }
- void CDllClass::MSG(const char * const str)
- {
- MessageBox(NULL,str,"",MB_OK);
- }
2: copy the compiled DLL files, Lib files, and Class header files to the project directory.
3: Modify the Class header file to convert the dll header file to dllexport and the application file to dllimport)
- // Import a class (including its methods and attributes)
- Class _ declspec (dllimport) CDllClass {
- Public:
- CDllClass (void );
- Void MSG (const char * const str );
- };
4: add the Class header file and the reference of the Lib file to the file that calls the class implementation function.
- # Include "DllClass. h" // introduce the header file, which is an object that can be defined by a program.
- # Pragma comment (lib, "DrawDll. lib") // define a function
It can be used normally. The above is the introduction to C ++ conventional DLL.