have been working on the DLL, the daily work is to debug a DLL, to the DLL to add their own code, but for the DLL has not been very understanding AH. Today, I found that I did not understand some of the basic knowledge of DLL writing. To learn, this article first summarizes the DLL's method of exporting functions.
1. First of all, how to build a common DLL project. (Take VS2008 as an example)
New Project--> Win32 tag--> fill in the project name--> dot OK, go to create Widzard--> next Enter second step--> application Type Select DL L radio button. --> in the additional options can be based on their own needs, to determine whether the production of empty engineering, the need to export symbols.
are not checked, later completely add themselves, see is a DLL framework is created, only a DLL function, when the DLL is loaded, by the thread call the function, do initialization. As shown in the following illustration:
Very simple, not much to say.
Check out the different options for the situation, no longer explain that you can try to create, 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 default created file is. cpp, which is the default compilation method of C + +. Therefore, in general, write functions at any time to declare functions, at any time to use, this is the benefit of C + + compilation, but also many books on the proposed variable use, to use the definition, declaration. If it is a. c file, it will be compiled by VS in C. What difference does it make. Is the function, the name of the variable named difference. When the compiler compiles the source file, the symbols used in the source file are modified, and C is underlined in the front of the symbol (symbols, function name, variable name, and so on), and for the function name, followed by the @num,num for the reference. and C + + in order to achieve function overload, the function of the implementation of the variable name mechanism (specific mechanism no longer explained, you can search for), such as function name, in the variable name will reflect the function source code name, function parameters, parameter type, return value and other information. So DLL libraries compiled in C are not directly connected by programs that cannot be compiled using C + +, and vice versa. In programming, there is an extern "C" appearance. If you are in a C + + project, to invoke a DLL library compiled in C, you need to declare the functions you want to import in an extern "C" so that they are compiled in C + + project as the function name of the compiled method, and you can find the corresponding symbol, link, in the Lib Library of C. Conversely, DLLs that are compiled for C + + cannot be invoked in a static link in a C program, primarily or because of C-compiled functions that cannot be linked to C + +-compiled DLLs and corresponding lib. C + + and C in the name of the specific content, but also the way the function is called __cdecl/__stdcall/_fastcall Several ways of modifying the symbols are also different.
In this case, however, you can use the GetProcAddress function to get the address of the function based on the function name, and then call the corresponding function in a dynamically introduced way.
There are different interpretations on X64, and X64 compiled functions are not changed. The front is not underlined, the function name at the end of the parameter space is not the size, of course, it will not be similar to C + + change.
exporting by exporting symbols :
_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;
}
As described above, the MyFunction function will be exported by the DLL, and C + + compiled in the form of a function that is exported as follows (you can view it using the PE resolution Tool):
The following functions exported for C + + compilation are shown with a "?" at the beginning of the function name, with two @@ 加上 yahhdpad@z at the end, together representing the parameters of the function and the return value. @ @YA means that the function call is __cdecl, and the default invocation method. H means the return value is int, followed by the type of the argument list. (detailed content can be searched for)
The following functions are exported as C-compiled, with only function names.
As mentioned above, in order to be able to make C + + compiled modules capable of being invoked in C and C + + programs, it is often defined as the following macros for declaring functions. On the one hand, the function is exported, while the C + + program compiles, or the DLL compiled by the compiler in C is the way to export the function. This will satisfy both C and C + + programs that can be invoked.
#define DLLEXPORT_API extern "C" _declspec (dllexport)
Or
#ifdef __cplusplus
extern "C" {
#endif
//function name to be exported
#ifdef __cplusplus
};
#endif
To set the export function from the link option :
By exporting functions through the linker option, you can rename the function so that the name exported in C + + compilation will be exported to a specified name without the need for a. @ and other characters of the function formerly known.
#pragma COMMENT (linker,/export:myfunction=?) myfunction@ @YAHHDPAD @z ")
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;
}
As shown in the following illustration, 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 can be easily referenced.
To export a function through a. def file :
The most formal method is to export the function by defining the. def file. The function to be exported, written to the. def file exports field, can be exported. In the connector phase, use the/DEF Connector option to invoke the. def file. The content of Def corresponds to the link option, and the. def syntax is as follows:
x statements, attributes, keywords, and specified identifiers are case-sensitive
X uses one, multiple spaces, tabs, or newline characters to separate statement keywords from other parameters and to separate statements. You can have 0, multiple spaces, tabs, or linefeed before and after the colon or equal sign of the specified parameter.
XName and LIBRARY statements must precede all other statements.
X comment begins 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.
=internalname indicates that a function named InternalName is exported as a function of entryname.
@ordinal allows you to specify an ordinal export function instead of a function name. The. lib file contains a mapping between ordinal and function.
Noname is optional, which indicates that only export by ordinal is allowed.
The PRIVATE option, which means that entryname is prohibited from being placed in the import library generated by link.
The data option, which indicates that the export is a database, not a code.
Example:
Exports
dllcanunloadnow @1 PRIVATE data
dllwindowname = Name data
DllGetClassObject @4 NONAME PRIVATE
dllregisterserver @7
DllUnregisterServer
LIBRARY Options
library [library] [base=address]
The library says let link create the DLL and create the 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 sections, such as setting the section as a shared section.
3. DLLs based on the MFC framework
It's easy to go 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 sum up. Interested can look for information.
ATL also has a lot, based on the ATL COM module, basically is to build a DLL module. This piece does not want to say more, but is the ATL framework, the template integration into the DLL as a container. In fact, this has nothing to do with the knowledge of the DLL, completely different two parts of the content, need to learn separately. I think it's purple, haha.
May meet later in the work, meets again. ^_^! With the principle of sufficiency, there is no memory of learning much.
by Andyguo @ 2015-08-11 afternoon