DLL's export and import

xiaoxiao2021-03-06  43

DLL's export and import

The DLL's export refers to the outputs of functions and data in the DLL to other programs for their use. The DLL's import refers to the functions and data in the DLL of the program using the DLL.

DLL's export

A table is included in the DLL called Export Table (hereinafter referred to as ET), which contains all functions and data of the DLL can be used by an external program. Only functions and data recorded in the ET can be used by an external program (if there is no .def file), all other functions and data not recorded in the ET are considered to be DLL private. Therefore, there are only two ways to use functions and data exports in the DLL:

l Create a .def file for the DLL (module definition file) and use this .def file when Build is DLL. Using this method allows you to press the function to export.

l Adding a _Declspec (DLLEXPORT) keyword (for function and variable definition, adding the most in front;) It will be added to the ET. Use this method function will press the name export.

Under Windows, no matter which method described above, you must declare the Export function as _stdcall.

About C and C compatibility issues

If you want to write C and C compatible DLL, because different name modification rules and different calls are used under C and C , if the DLL is written and compiled with C, the function is used when used for C modules Before the declaration, the extern "C" keyword should be added to tell Linker to use the C external connection (ie, looking for functions in the external module); in turn, if the DLL is written and compiled with C , When the function is declared in the C module, add the extern "C " keyword. VC identifies the C program by _cplusplus macro. If it is a C program, the VC compiler will define the _cplusplus macro. Therefore, in the DLL, the following technique can be used to solve the compatibility problem:

#ifdef _cplusplus

Extern "C" {

#ENDIF

// Put all the functions

#ifdef _cplusplus

}

#ENDIF

.Def file

The .def file is a text file that contains the DLL module information. The syntax structure is as follows:

Library DLL File Name

Description "Descriptions"

Exports

Function name @nums

Library is a keyword, followed by the associated DLL file name; Description is an optional description string, in addition to adding readability, it is useless; exports is the list of Export functions, first is a function name, then @ symbol After the number of times the number, the number of the function, the number of the function, from 1 to DLL, the total number of Export functions. Note that the name here is a function name after name is called, if the DLL is written with C , it is very depressed.

If it is extended DLL (Extension DLL), and through the .def file export, you must add the following statement to the header file: #undef AFX_DATA

#define AFX_DATA AFX_EXT_DATA

// other content in header files

#undef AFX_DATA

#define AFX_DATA

These statements ensure that variables used in some MFCs are used in Export to an external program. For example: the CruntimeClass variable obtained by Declare_Dynamic in the class. Otherwise, the DLL will not be able to compile and connect properly, or the external program cannot be connected correctly to the DLL.

DLL's import

One source file in an external program uses functions and data in the DLL, just like the functions and data in the external module, must first give a declaration of functions and data; for the Class to give the class definition. This is called Import. For the VC compiler, the syntax of the import DLL and the syntax of the data is similar to the general declaration, but you want to add the _Declspec (dllimport) keyword (for function and variable declaration, adding the front; for Class definition, add After the Class Keyword). If it is a function, the keyword is optional, but the keyword can cause the compiler to generate more efficient code. But for variables and classes, this keyword must be used.

By using the following technique, you can write a header file universal in the .lib file and an external program source file:

#ifdef _exporting

#define class_declspec __declspec (dllexport)

#ELSE

#define class_declspec __declspec (dllimport)

#ENDIF

The _EXPORTING macro can be used for the label file from the DLL file or an external program.

转载请注明原文地址:https://www.9cbs.com/read-58334.html

New Post(0)