The add project, the add. Add in the DLL function is forwarding is NtTestDll NtTestDll program. DLL export NtAdd function, but just test used to compile don't pass, I don't know what reason be?
This is the add. H header file
# pragma comment (would, "/export: add=NtTestDll. NtAdd")
Extern "C" __declspec (dllexport) int the add (int a, int b);
Add the CPP source file
# include "the add. H"
Test compile without
CodePudding user response:
DLL export function name the thingsKey words: vc + + DLL export functions
Often use VC6 Dependency check the name of the DLL export functions, can find the name of the DLL export functions are sometimes very different, the cause of different mostly and compile DLL when specified by the qualifier with the DLL export functions,
Vc + + to support two languages: C/C + +, this also is the root cause of the differences in the DLL export functions
We use VS2008 new DLL project, a project called "TestDLL"
The default source file suffix. CPP instead. C (C)
Enter the test code is as follows:
01 int _stdcall MyFunction (int iVariant)
02 {
03 return 0;
04}
In order to export the function above, we have the following several methods:
1. Using the traditional (. The module definition file (def)
A new suffix for. Def text file (here to build a TestDll. Def), the file content is:
The LIBRARY TestDll
EXPORTS
MyFunction
When the Link specified depends on input file:/DEF: "TestDll. DEF
"2. Visual c + + provides convenient way
Join before 01 done int __declspec (dllexport) keyword
Through the above two methods, we can export MyFunction function,
We use the Dependency check the exported functions:
The first method the exported functions as follows:
MyFunction
The second method the exported functions as follows:
_MyFunction @ 4
__stdcall can make the export function to add an underscore the names, followed a @ plus the number of bytes parameters, such as _MyFunction @ 4 parameters (int iVariant) is 4 bytes
__fastcall as __stdcall similar, but not in front of the underline, but a @, such as @ MyFunction @ 4
__cdecl is beginning the function name,
Summary: if you want to export the function in the C file, and don't let the compiler function name, def file is used to derive the function,
Let's take a look at c + + files
We use VS2008 new DLL project, a project called "TestDLL"
The default source file suffix for the CPP (namely c + + files),
Enter the test code is as follows:
01 int _stdcall MyFunction (int iVariant)
02 {
03 return 0;
04}
In order to export the function above, we have the following several methods:
3. Use the traditional (. The module definition file (def)
A new suffix for. Def text file (here to build a TestDll. Def), the file content is:
The LIBRARY TestDll
EXPORTS
MyFunction
When the Link specified depends on input file:/DEF: "TestDll. DEF
"4. Visual c + + provides convenient way
Join before 01 done int __declspec (dllexport) keyword
Through the above two methods, we can export MyFunction function,
We use the Dependency check the exported functions:
The first method the exported functions as follows:
MyFunction
The second method the exported functions as follows:
? MyFunction @ @ YGHH @ Z
Can see the second method to get the export function name is not what we want, and if used display method in exe MyFunction (LoadLibrary and GetProcAddress call) call will fail,
But with the import library (*. LIB) calls, the compiler automatically convert the function name, so there is no problem, always
Solve the problem of method is:
Using VC preprocessor directive to "# pragma" option to specify links,
As follows:
# pragma comment (would, "/EXPORT: MyFunction=? MyFunction @ @ YGHH @ Z ")
At this moment, you will find the exported function name in the table have we want MyFunction, but we found the original? MyFunction @ @ YGHH @ Z function is still there, then you can put the __declspec () modified to remove, you just need to pragma directive,
But also can make the following format:
# pragma comment (would/EXPORT: MyFunction=_MyFunction @ 4, "PRIVATE")
The role of PRIVATE and its role in the def file, more # pragram please see MSDN,
Summary: if you want to export functions in c + + files, and don't let the compiler function name, def file is used to derive the function,
At the same time can use the # pragma instruction can also be used in (C),
Conclusion:
In c + + compiler to generate DLL, adaptation, the name of the exported functions and different compiler USES adaptation rules are different, so the adaptation after name are different (typically involves the overloading in c + +, etc.),
If using different compiler generated DLL and visit DLL exe program respectively, the latter in to access the DLL's exported functions can be a problem, as above case MyFunction () function in c + + compiler adapted after's name is? MyFunction @ @ YGHH @ Z, we hope that the name of the compiled no change, there are several methods,
The first method is through a called DEF module definition files to resolve,
The LIBRARY TestDll
EXPORTS
MyFunction
LIBRARY is used to specify dynamic link LIBRARY internal name, the name and generate dynamic link LIBRARY name must match, this code is not necessary,
EXPORTS illustrates the DLL will export function, as well as for the export function specified symbol name,
The second is to define the exported functions plus qualifiers: extern "C"
Such as: # define DLLEXPORT_API extern "C" _declspec (dllexport)
But extern "C" only solved the call between C and C + + language problems (extern "C" is tells the compiler, and make it the way the C compiler), it can only be used for export global function this kind of situation and cannot export a class member function,
If the export function calling conventions change at the same time, even using extern "C", the compiled function name will change, for example above we join _stdcall keyword description calling conventions (standard calling conventions, namely WINAPI calling convention),
# define DLLEXPORT_API extern "C" _declspec (dllexport)
01 DLLEXPORT_API int _stdcall MyFunction (int iVariant)
02 {
03 return 0;
04}
The compiled function MyFunction adapted _MyFunction @ 4
By the first method is the module definition file compiled DLL export function name won't change,
DLL (dynamic library) export function name garbled meaning
C + + compiler function name modified agreed rules:
__stdcall calling convention:
1, in order to "?" Identify the start of the function name, followed by the function name;
2, behind the function name, with the beginning of the "@ @ YG" table that identifies a parameter, followed by the parameter list;
3, the parameter list to code said:
X - void
D - char
E - unsigned char
F - short
H - int
I, unsigned int
J - long
K - unsigned long
M - float
N - double
_N - bool
.
PA - said a pointer, the back of the code that pointer type, if the same type of pointer appears in a row, to "0" instead of a "0" is for a repeat;
Table 4, the parameters of the first item for the function return value type, followed by subsequent parameter data types, pointer id before referred to in the data type;
5, parameter table to identify the name "@ Z" after the end of this function has no parameters, ends with a "Z" logo,
The format for? "" Functionname @ @ YG "or" * * * * * @ Z? Functionname @ @ YG * XZ ", for example,
Int Test1 (char * var1, unsigned long) -- -- -- -- -- "? Test1 @ @ YGHPADK @ Z "void Test2 () -- -- -- -- --"? Test2 @ @ YGXXZ
"
__cdecl calling convention:
Rules ditto below _stdcall calling convention, is just the beginning of the argument list identifying the above "@ @ YG" into "@ @ YA,"
__fastcall calling convention:
Rules ditto below _stdcall calling convention, is just the beginning of the argument list identifying the above "@ @ YG" into "@ @ YI,"
If you want to use DEF file output a "c + + class," is to output data, and members of the decorated names written. DEF module definition file
So... nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull