Some problems about DLL
There are often friends asking questions about DLL in the forum, and now make a summary: 1. Call the DLL output function in the program, illegal operation (stack error) This problem is generally due to the actual definition of functions. Inconsistently caused. To figure out this problem, you must understand the call mechanism of the function. In a Windows system, there are two ways: C and Pascal. C / C uses a C method, that is, the parameter is stacked by the right direction, and the caller is responsible for the restore of the stack. This is the advantage of implementing the number of variable parameters, typically scanf / printf; another It is a PASCAL method, that is, the parameter is also from right to left, but by the function of the stack of restorations, such the callback does not use the stack, saving coding (without using the stack restore instructions in each function) ). In C / C , the default call mode is C, also called _cdecl. To use the PASCAL mode, you must display it as Pascal (in the VC, you can use _stdcall / pascal / winapi / callback, actually a thing). In fact, in the DLL, the calling method also affects the name of the function, this is not discussed for the time being, but it is mentioned below.
We can see the difference between C and Pascal in two ways (pay attention to the "***************" void function1 (DWord V1, DWORD V2) {10012560 Push EBP 10012561 mov ebp, esp 10012563 sub esp, 0C0h 10012569 push ebx 1001256A push esi 1001256B push edi 1001256C lea edi, [ebp-0C0h] 10012572 mov ecx, 30h 10012577 mov eax, 0CCCCCCCCh 1001257C rep stos dword ptr [edi]} 1001257E pop edi 1001257F POP ESI 10012580 POP EBX 10012581 MOV ESP, EBP 10012583 POP EBP 10012584 RET // *************** VOID WINAPI FUNCTION2 (DWORD VI, DWORD V2) {10012590 Push EBP 10012591 MOV EBP , esp 10012593 sub esp, 0C0h 10012599 push ebx 1001259A push esi 1001259B push edi 1001259C lea edi, [ebp-0C0h] 100125A2 mov ecx, 30h 100125A7 mov eax, 0CCCCCCCCh 100125AC rep stos dword ptr [edi]} 100125AE pop edi 100125AF pop esi 100125B0 POP EBX 100125B1 MOV ESP, EBP 100125B3 POP EBP 100125B4 RET 8 // ************* Two DWORD parameters, total eight bytes Function1 (1, 2); 10012604 Push 2 // ********* C mode, from right to left in the stack 10012606 Push 1 10012608 Call function1 (10011749h) 1001260D add ESP, 8 /// ********* The caller is responsible for the stack function2 ( 1, 2); 10012610 Push 2 // ************ PASCAL mode When the DLL is dynamically loaded, it is found that the output of the output cannot be positioned with the GetProcAddress positioning. It may be two possible: First, the function is not output at all, and the other is the name of the output function. These two reasons are caused by the incorrect processing of writing a DLL. The keywords related to the DLL in the VC are: _Declspec, dllexport, DLLIMPORT, EXTERN "C". Another point is the problem of .def definition file. See the relevant documentation in the format of the .def file.