Dynamic link library (DLL)

Source: Internet
Author: User

Dynamic Link Library : We often make the common code into an executable module for other executable file calls, such a module called the link library, is divided into dynamic link library and static link library.

For the static link library, Lib contains the implementation code and will be included in the EXE, resulting in a large file, wasting disk and memory;

For dynamic-link libraries, DLLs do not have to be included in the final EXE, and EXE can dynamically mount and unload DLL files.

Export function

There are two ways to declare a function as an export function:

1. Add _declspec (dllexport) to the function declaration:

such as: extern "C" Int__declspec (dllexport) Add (int x, int y);

2. Declare in a. def file: for example:

; Lib.def: Exporting DLL functions

LIBRARY Dlltest

Exports

ADD @ 1

with ";" Starting with the comment line, the above example shows: Generate a dynamic link library named "Dlltest", export the Add function, the ordinal number is 1. The sequence number functions when the function is called: GetProcAddress (hDLL, Makeintresource (n)) returns the Nth function address defined by def.

DLL calls

When you use a dynamic-link library, you often provide two files: Introducing library files (LIB) and dynamic-link libraries (DLLs). The Introduction library file (LIB) contains the symbolic names and addresses of all exported functions and variables of the dynamic Connection library (DLL), and the Dynamic Connection library (DLL) contains the actual functions and data. The invocation method is divided into dynamic invocation (explicit linking) and static invocation (implicit linking).

1. Static call (implicit link)

The loading and unloading of DLLs is done by the compiling system. A total of two steps are required:

Tell the compiler the path and file name of the Lib file that corresponds to the DLL, such as #pragma comment (lib, "DllTest.lib");

Declare the import function, such as extern "C" __declspec (dllimport) Add (int x,int y) (if a header file with an exported function declaration can directly contain the. h file);

2. Dynamic call (show link)

Use the Win32 API function LoadLibrary, GetProcAddress, freelibrary implement "DLL load-dll function address get-dll release". This method does not require LIB files, only the DLL files, explicit links faster and more flexible.

About DLL Placement paths:

Using explicit linking, you can specify the full path to the DLL file in the parameters of the function LoadLibrary, or if you do not specify a path or implicitly link, Windows will follow the following search order to locate the DLL:

(1) directory containing EXE files

(2) Project catalogue

(3) Windows system directory

(4) Windows directory

(5) A list of directories listed in the PATH environment variable

Cases:

1. Build DLL

Create the DLL project for the WIN32 console mydll, new mydll.h and Mydll.cpp, the source code is as follows:

mydll.h file #ifndef _mydll_h#define _mydll_hextern "C" int __declspec (dllexport) Add (int x, int y); #endif
Mydll.cpp file # include "mydll.h" int Add (int x, int y) {return x + y;}

Compile (F7) to generate MyDll.lib and MyDll.dll. (extern "C" specifies that the exported function is a C link, which avoids inconsistent function names due to the different naming conventions of C + +)

2. Calling DLL

Create Win32 Console project, new Test.cpp file, source code as follows:

Dynamic invocation: (copy MyDll.dll to the project directory)

#include <stdio.h> #include <windows.h>typedef int (*lpaddfun) (int, int); Macro definition function pointer type int main (int argc, char *argv[]) {hinstance hDLL = LoadLibrary ("MyDll.dll");//Load DLL if (hdll! = NULL) {Lpaddfun A Ddfun = (lpaddfun) GetProcAddress (hDLL, "add");//Gets the function address if (addfun! = NULL) {int result = Addfun (2, 3);p rintf ("%d", result); }freelibrary (hdll);//uninstall Dll}return 0;}

static invocation: (copy MyDll.dll and MyDll.lib to the project directory)

#pragma comment (lib, "MyDll.lib") extern "C" __declspec (dllimport) int add (int x,int y); #include <stdio.h>int main (int argc, char* argv[]) {int result = Add (2,3); printf ("%d", result); return 0;}

Sometimes DLL export function if many, in the static call, in order to avoid writing out the extern "C" __declspec (dllimport) function (), you can directly include the DLL header file (if any). For DLL makers, it is necessary to implement an automatic conversion of whether the function in the header file is imported or exported, which can be achieved by defining the macro. In the DLL project, the header file acts as an export declaration, and in the Application project the header file is the export declaration. At this time, DLL project Mydll source code as follows:

mydll.h file #ifndef _mydll_h#define _mydll_h#ifdef mydll_export#define mydll_api __declspec (dllexport) #else # define Mydll_api __declspec (dllimport) #endifextern "C" Mydll_api int Add (int x, int y); #endif
Mydll.cpp File # define Mydll_export//define this symbol only in the DLL implementation # include "mydll.h" int Add (int x, int y) {return x + y;}

Static call: (copy mydll.dll,mydll.lib, mydll.h to project directory)

#pragma comment (lib, "MyDll.lib") #include "mydll.h" #include <stdio.h>int main (int argc, char* argv[]) {int result = Add (2,3); printf ("%d", result); return 0;}

Reference:

http://www.cnblogs.com/chio/archive/2007/11/03/948480.html#undefined

Http://www.cnblogs.com/fangyukuan/archive/2010/06/20/1761464.html

Http://www.cppblog.com/amazon/archive/2009/09/04/95318.html

Dynamic link library (DLL)

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.