A brief analysis of the creation and invocation of dynamic link libraries in C + + _c language

Source: Internet
Author: User

DLLs help you share data and resources. Multiple applications can access the contents of a single DLL copy in memory at the same time. A DLL is a library that contains code and data that can be used by multiple programs at the same time. The following is a brief introduction to the creation and invocation of dynamic link libraries in C + +.

Steps to create a dynamic connection library:

There are two ways to create a DLL.

First, create NON-MFC DLL dynamic link library

1, open file-> new-> project option, select Win32 dynamic-link library->sample project-> project name: Dlldemo

2, a new one. H file DllDemo.h

#ifdef dlldemo_exports  
#define DLLAPI __declspec (dllimport)  
#else  
#define DLLAPI __declspec (dllexport) 
extern "C"//compiled  
as is { 
  Dllapi int __stdcall Max (int a,int b);//__stdcall enable the ability to invoke APIs in non-C + + languages 
#endif

3, import DllDemo.h file in DllDemo.cpp file, and implement Max (Int,int) function

#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 dynamic connection library

II. Create a dynamic connection library with a. def file DllDemo.dll

1, delete the Dlldemo project in the DllDemo.h file.

2, in the DllDemo.cpp file header, delete #include DllDemo.h statement.

3. Add a text file to the project, name it dlldemo.def and write the following statement:

1.LIBRARY Mydll
2.EXPORTS
3.max@1

4, compile the program to generate dynamic Connection library.

Call steps for dynamic Links:

One, implicit invocation of

1, the establishment of Dllcnsltest project

2, the file DllDemo.dll, DllDemo.lib Copy to the Dllcnsltest project directory

3, add the following statement in the DllCnslTest.h:

#define DLLAPI __declspec (dllimport)  
#pragma comment (lib, "DllDemo.lib")//link to DllDemo.lib file  
when editor link extern "C"  
{  
dllapi int __stdcall Max (int a,int b);  
}

4, add the following statement in 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 build the application DllCnslTest.exe

Second, the explicit invocation of

1, the establishment of Dllwintest project.

2. Copy the file DllDemo.dll to the directory of the Dllwintest project or the Windows system directory.

3. Use the Dumpbin.exe applet under Vc/bin to view the function structure in the DLL file (DllDemo.dll).

4. Use the type definition keyword typedef to define pointers to the same function prototypes in the DLL.

Cases:

Need to introduce header file Windows.h best at the front, or you may be wrong  do not need windef.h otherwise will be an error 
#include <windows.h>
Must be declared as __stdcall type otherwise error

5. Load the DLL into the current application through Loadlibray () and return the handle to the current DLL file.

Cases:

HINSTANCE hDLL; Declaring a DLL instance file handle  

6. Get the function pointers imported into the application through the GetProcAddress () function.

Cases:

Lpmax M;  
M = (Lpmax) GetProcAddress (hDLL, "Max");  
int value;  
Value = M (2,9);  

Note: max = (Lpmax) GetProcAddress (hDLL, "Max") in the above code; Where the parameter "Max" is a function name and may not be found (possibly related to a module definition file that is not used). At this point, you can use two ways to resolve

1, using vs from the command line tool to view the correct name of the function, first CD to the bin directory (Vc/bin), using the VS Dumpbin.exe tool. After entering the bin directory, enter the command dumpbin/exports d:\ path \xxx.dll to view the output name of the function. Typically a _functionname@ number, in this case the name is _max@8.

2. Depending on the function in the DLL (the order in which the DLL is generated in the header file), such as Max = (Lpmax) GetProcAddress (hDLL, (LCPSTR) Makeintresource (1)); \ \ Find the first function

7, after the function call completes, uses FreeLibrary () to unload the DLL file.

FreeLibrary (hDLL);

8. Compile and build the application DllWinTest.exe

Note: You do not need to use the appropriate LIB file when compiling an explicit linked application.

Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. By using DLLs, programs can be modular and consist of relatively independent components. I hope that through this analysis, you will have an understanding of this.

The above analysis of C/s + + dynamic link library creation and invocation is a small set to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.