1, Phenomenon:
A custom C + + DLL that compiles without errors, another program that uses dynamic calling methods, loads DLLs normally, but does not succeed when calling functions using various methods.
Using the DLL view tool, it is found that the function is named Func and garbled combinations, such as:
2, Reason lookup:
All parties look for reasons, found that the reason is not some of the posts said to the compiler DLL in the header file to add extern "C"
extern "C" simply means that the export function uses the C compiler and does not add extern "C" instructions to use C + + compiler rules.
The two rules differ as follows:
(1) C compiler's function name decoration rules
For the __stdcall calling convention, the compiler and linker precede the output function name with an underscore prefix, followed by a "@" symbol and the number of bytes of its argument, and if the original function is Func (), the compiler becomes [email protected];
(2) C + + compiler's function name decoration rules
C + + 's function name decoration rule content is more rich, including function name, return value type, parameter type and other information.
A, regardless of the __cdecl,__fastcall or __stdcall invocation, the function decoration takes a "?" Begin
B, followed by the name of the function
This is followed by the start identification of the parameter table and the parameter table spelled out by the parameter type code.
C, the detailed rules are as follows: __stdcall, the starting identifier for the parameter table is "@ @YG"
The __cdecl way is "@ @YA"
The __fastcall method is "@ @YI".
d, the parameter list is indicated by the code name:
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--represents the pointer, followed by the code indicates the pointer type, if the same type of pointer appears consecutively, with "0" instead of a "0" represents a repetition;
The first item of the parameter table is the return value type of the function, followed by the data type of the parameter, and the pointer is identified before the data type it refers to;
E, after the parameter table, "@z" identifies the end of the entire name, and if the function has no arguments, the "Z" Mark ends.
extern "C" only solves the problem of calls between C and C + + (extern "C" is to tell the compiler to compile it in C), it can only be used to export global functions and cannot export a member function of a class.
At the same time, if the calling convention of the exported function changes, the compiled function name will change even if the extern "C" is used.
3. Correct method
Add a module definition file (def file):
LIBRARY "Dodll"
Exports
Test1 @ 1
Test2 @ 2
Simultaneous output in release mode
Call Normal
The function name is garbled (such as @[email protected]@[email protected]) for the reason and solution after the custom C + + DLL export