Always tinker DLL, the daily work is to debug a DLL, to add their own code inside the DLL, but for the DLL has been not very understanding AH! Today, a look at the information, only to find that they have some basic knowledge of DLL writing also do not understand. To learn, this article first summarizes the methods of DLL export functions.
1. Start by saying how to build a common DLL project! (Take VS2008 as an example)
New Project--Win32--fill in the project name--click OK, go to create Widzard--next go to step two--application Type Select DL L radio button. --In additional options, you can determine whether to generate an empty project or to export a symbol, depending on your needs.
are not checked, later completely self-added, see is a DLL framework is created, only a DLL function, when the DLL is loaded by the thread called the function, do the initialization. As shown in the following:
Very simple, not much to say!
Tick the different additional options, no longer explain, you can try to create a bit, see what the difference.
2. Three ways to export symbols in a module
Before you say three ways to export, say something about the VS compiler. vs compiler, the file created by default is. cpp, which is the default method of compiling C + +. Therefore, in general, write functions at any time to declare functions, ready to use, this is the benefits of C + + compilation, but also many books advocated the use of variables, to use the definition, declaration. And if it is a. c file, it will be compiled by VS as C. What difference does it make? Is the function, the name of the variable names difference. After the compiler compiles the source file, the symbols used in the source file are modified, and C is underlined in front of the symbol (Symbols, function name, variable name, etc.), and for the function name, followed by the size of the @num,num for the argument. C + + in order to implement function overloading, the function of the implementation of the variable name mechanism (the specific mechanism is no longer explained in detail, you can search), such as the function name, in the variable name will reflect the function source code name, function parameter number, parameter type, return value and other information. therefore DLL libraries compiled in C are not directly connected by programs compiled using C + +, and vice versa. In programming, there is the appearance of extern "C". If a C + + project is to invoke a DLL library compiled from the compiler, you will need to declare the functions to be imported in the form of extern "C", so that these functions are compiled in C + + projects into a C-compiled function name, and the corresponding symbols and links can be found in the Lib Library of C. In turn, for a C + + compiled DLL, it cannot be statically linked in a C program, or the main reason is because the C compiler compiled the function, unable to link to C + + compiled DLL, and the corresponding lib. The specific contents of the variable names in C + + and C, as well as the __cdecl/__stdcall/_fastcall in several ways, are also different from the way the function is called.
In this case, however, it is possible to use the GetProcAddress function to get the address of the function based on the function name, and then invoke the corresponding function, in a dynamically introduced way.
There are different explanations on X64, and the X64 compiled function does not change the name. There is no underscore in front, there is no parameter space at the end of the function name, and of course it does not resemble C + + 's variable name.
Export by exporting symbols :
[CPP] view plain copy print?
- _declspec (dllexport) int MyFunction (int A, char C, char* pInfo)
- {
- char Szinfo[max_path] = {};
- sprintf (Szinfo, "a=%d, C=%c, Info:%s", A, C, pInfo);
- Outputdebugstringa (Szinfo);
- return 0;
- }
_declspec (dllexport) int MyFunction (int A, char C, char* pInfo) {char Szinfo[max_path] = {};sprintf (szinfo, "a=%d, c=%c, in Fo:%s ", A, C, PInfo); Outputdebugstringa (szinfo); return 0;}
As described above, the MyFunction function will be exported by the DLL, and C + + compiled in the form of a function exported as follows (can be viewed using the PE parsing tool):
The following is a C + + compilation method of the exported function, you can see the function name at the beginning of a "?", at the end of the two @@ 加上 [email protected], they together represent the function parameters, as well as the return value. The @ @YA means that the function is called __cdecl, the default invocation method. H means the return value is int, followed by the type of the parameter list. (detailed content can be searched for a bit)
The following functions are exported as C compilation, with only the function name.
As mentioned above, usually in order to enable C + + compiled modules can be called in C and C + + programs, usually define the following macro, used to declare the function. On the one hand, the function is exported, at the same time, whether the C + + program compiles, or the DLL compiled by the compiler C compiler to export the function. This allows both C and C + + programs to be called.
#define DLLEXPORT_API extern "C" _declspec (DLLEXPORT)
Or
[CPP] view plain copy print?
- #ifdef __cplusplus
- extern "C" {
- #endif
- The name of the function
- #ifdef __cplusplus
- };
- #endif
#ifdef __cplusplus extern "C" { #endif//function name to be exported #ifdef __cplusplus }; #endif
To set the export function with the link option :
Using the linker option to export a function, you can rename the function, export the name in C + + compilation to a specified name instead of a. The function of the @ equal character is formerly known.
[CPP] view plain copy print?
- #pragma COMMENT (linker, "/export:[email Protected]@[email protected]")
- int MyFunction (int A, char C, char* pInfo)
- {
- char Szinfo[max_path] = {};
- sprintf (Szinfo, "Dll module:a=%d, C=%c, Info:%s\n", A, C, pInfo);
- Outputdebugstringa (Szinfo);
- return 0;
- }
#pragma COMMENT (linker, "/export:[email Protected]@[email protected]") int MyFunction (int A, char C, char* pInfo) {char Szi Nfo[max_path] = {};sprintf (szinfo, "Dll module:a=%d, C=%c, Info:%s\n", A, C, PInfo); Outputdebugstringa (szinfo); return 0; }
As shown, the function in the above code, compiled in C + +, is the same as the name of the previously exported function in C + +, and we use the form of #pragma comment to export the function as the original name of the function. This makes it easy to reference.
To export a function through a. def file :
The most formal method, or to export a function by defining a. def file. The function to be exported is written to the. def file exports field, which you can export. In the connector stage, use the/DEF Connector option to invoke the. def file. The DEF content corresponds to the link option, and the. def syntax is described below:
x statement, attribute, keyword, and specified identifier are case-sensitive
X uses one, multiple spaces, tabs, or newline characters to separate the statement keyword from other parameters and to separate statements. You can have 0, multiple spaces, tabs, or line breaks before and after the colon or equal sign of the specified parameter.
The XName and LIBRARY statements must precede all the other statements.
X comment starts with a semicolon
The syntax for the EXPORTS statement is as follows:
Exports
Entryname[=internalname] [@ordinal [NONAME]] [PRIVATE] [DATA]
EntryName is the name of the function or variable to be exported. This one must have.
The =internalname represents a function that exports a function named InternalName to EntryName.
@ordinal allows you to specify that an ordinal export function is not exported as a function name. The. lib file contains the mapping between the ordinal and the function.
Noname is optional, which indicates that only export by ordinal is allowed.
PRIVATE option, which means that entryname is forbidden to be placed in the import library generated by link.
The data option, which indicates that the export is to the database, not the code.
Example:
[CPP] view plain copy print?
- Exports
- DllCanUnloadNow @1 PRIVATE DATA
- Dllwindowname = Name DATA
- DllGetClassObject @4 NONAME PRIVATE
- DllRegisterServer @7
- DllUnregisterServer
Exports dllcanunloadnow@1privatedata dllwindowname = namedata dllgetclassobject@4nonameprivate Dllregisterserver@7dllunregisterserver
LIBRARY Options
library [library] [base=address]
The library says let link create a DLL and create an import library.
Base=address sets the base address that the operating system uses to load DLLs.
Sectioins Options
SECTIONS
Definitions
Used to specify the properties of some section areas, such as setting the section to a shared section area, and so on.
3. DLLs based on the MFC framework
It is easy to get around global variables, export functions, and export classes. With the support of MFC, a lot of convenience. This piece is not how to use it, I usually difficult to relate to, do not want to summarize. If you are interested, you can look for information.
ATL also has many, atl-based COM modules, basically building a DLL module. This piece does not want to say more, nothing more than the ATL framework, template integration into the DLL as a container. In fact, this and DLL knowledge does not matter, completely different two parts of the content, need to separate learning. I think it is jiangzi, haha haha!
Maybe later in the work will meet, meet again! ^_^! Take enough as the principle, learn too much also can't remember!
by Andyguo @ 2015-08-11 Noon
How MFC DLL export functions are defined