Create and call dynamic link library in C/C ++

Source: Internet
Author: User

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

 
 
  1. # Ifdef DllDemo_EXPORTS
  2. # Define DllAPI _ declspec (dllexport)
  3. # Else
  4. # Define DllAPI _ declspec (dllimport)
  5. Extern "C" // compiled as is
  6. {
  7. DllAPI int _ stdcall Max (int a, int B); // _ stdcall enables non-C/C ++ languages to call APIs
  8. }
  9. # Endif

3. Import the DllDemo. h file in the DllDemo. cpp file and implement the Maxint and int functions # include "DllDemo. h"

 
 
  1. DllAPI int __stdcall Max(int a,int b)   
  2. {   
  3. if(a==b)   
  4. return NULL;   
  5. else if(a>b)   
  6. return a;   
  7. else   
  8. return b;   
  9. }  

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:

 
 
  1. LIBRARY MyDll  
  2. EXPORTS  
  3. 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:

 
 
  1. # Define DllAPI _ declspec (dllimport)
  2. # Pragma comment (lib, "DllDemo. lib") // link to the DllDemo. lib file in the link editor.
  3. Extern "C"
  4. {
  5. DllAPI int _ stdcall Max (int a, int B );
  6. }

4. Add the following statement to the DllCnslTest. cpp file: # include "DllCnslTest. h" // or # include "DllDemo. h"

 
 
  1. void main()   
  2. {   
  3. int value;   
  4. value = Max(2,9);   
  5. printf("The Max value is %d\n",value);   
  6. }  

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:

 
 
  1. 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:

 
 
  1. HINSTANCE hDll; // declare a Dll instance file handle
  2. 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:

 
 
  1. lpMax Max;   
  2. Max = (lpMax)GetProcAddress(hDLL,"Max");   
  3. int value;   
  4. value = Max(2,9);   
  5. printf("The Max value is %d",value);  

7. After the function is called, use FreeLibrary) to uninstall the DLL file.

 
 
  1. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.