DLLIt helps to share data and resources. Multiple applications can simultaneously access the content of a single DLL copy in the memory. DLL is a library that contains code and data that can be used by multiple programs at the same time. Next we will introduce youC/C ++Create and call a dynamic link library.
To create a dynamic Connection Library, follow these steps:
1. Create a Non-mfc dll Dynamic Link Library
1. Open File-> New-> Project option, and select Win32 Dynamic-Link Library-> sample project-> Project name: DllDemo
2. Create a new one. H file DllDemo. h
- # Ifdef DllDemo_EXPORTS
- # Define DllAPI _ declspec (dllexport)
- # Else
- # Define DllAPI _ declspec (dllimport)
- Extern "C" // compiled as is
- {
- DllAPI int _ stdcall Max (int a, int B); // _ stdcall enables non-C/C ++ languages to call APIs
- }
- # Endif
3. Import the DllDemo. h file in the DllDemo. cpp file and implement the Maxint and int functions # include "DllDemo. h"
- DllAPI int __stdcall Max(int a,int b)
- {
- if(a==b)
- return NULL;
- else if(a>b)
- return a;
- else
- return b;
- }
4. Compile the program to generate a dynamic Connection Library
Ii. Use the. def file to create a dynamic Connection Library DllDemo. dll
1. Delete the DllDemo. h file in the DllDemo project.
2. In the DllDemo. cpp file header, delete the # include DllDemo. h Statement.
3. Add a text file named DllDemo. def to the project and write the following statement:
- LIBRARY MyDll
- EXPORTS
- Max@1
4. Compile the program to generate a dynamic Connection Library.
Call steps for dynamic links:
1. implicit call
1. Establish the DllCnslTest Project
2. Copy the DllDemo. dll and DllDemo. lib files to the directory where the DllCnslTest project is located.
3. Add the following statement to DllCnslTest. h:
- # Define DllAPI _ declspec (dllimport)
- # Pragma comment (lib, "DllDemo. lib") // link to the DllDemo. lib file in the link editor.
- Extern "C"
- {
- DllAPI int _ stdcall Max (int a, int B );
- }
4. Add the following statement to the DllCnslTest. cpp file: # include "DllCnslTest. h" // or # include "DllDemo. h"
- void main()
- {
- int value;
- value = Max(2,9);
- printf("The Max value is %d\n",value);
- }
5. compile and generate the application dllcnsltest.exe
Ii. explicit call
1. Create the DllWinTest project.
2. copy the file DllDemo. dll to the directory where the DllWinTest project is located or under the Windows System directory.
3. Use the dumpbin.exe applet under vc/binto view the function structure in the DLL file DllDemo. dll.
4. Use the type definition keyword typedef to define the pointer to the same function prototype as the DLL.
Example:
- Typedef int (* lpMax) (int a, int B); // This statement can be placed in a. h file.
5. Load the DLL to the current application through LoadLibray and return the handle of the current DLL file.
Example:
- HINSTANCE hDll; // declare a Dll instance file handle
- HDll = LoadLibrary ("DllDemo. dll"); // import the dynamic Connection Library of DllDemo. dll
6. Use the GetProcAddress function to obtain the function pointer imported to the application.
Example:
- lpMax Max;
- Max = (lpMax)GetProcAddress(hDLL,"Max");
- int value;
- value = Max(2,9);
- printf("The Max value is %d",value);
7. After the function is called, use FreeLibrary) to uninstall the DLL file.
- FreeLibrary(hDll);
8. compile and generate the application dllwintest.exe
Note: When explicitly linking an application, you do not need to use the corresponding Lib file during compilation.
Dynamic links provide a way for a process to call a function that does not belong to its executable code. By using DLL, the program can be modularized and composed of relatively independent components. I hope you will understand this through the analysis in this article.