DLL calls the agreement and name modification (2)

zhaozj2021-02-16  62

DLL calls the agreement and name modification (2)

4, THISCALL

The Thiscall call agreement is the default call agreement of the non-static class member function in C . Thiscall can only be used by the compiler, there is no corresponding keyword, so it cannot be specified by the programmer. When using thisCall Convention, the function parameters are in the stack from the right to left, and the called function is returned to the stack of the transfer parameters before returning, but additionally parameters are transmitted through the ECX register: this pointer.

This example will define a class and define a member function in the class, the code is as follows:

Class Csum

{

PUBLIC:

Int Add (Int A, INT B)

{

Return (A B);

}

}

void main ()

{

Csum Sum;

Sum.Add (1, 2);

}

Function call part assembly code:

Csum SUM;

Sum.Add (1, 2);

Push 2; parameter from right to left in the stack, first press 2

Push 1; press 1

Lea ECX, [EBP-4]; ECX stores this pointer

Call @ ilt 5 (CSUM :: Add) (

0040100A

); Call function implementation

Function implementation part assembly code:

; Int Add (int A, int b)

Push EBP

MOV EBP, ESP

SUB ESP, 44H; Murate a 4Bytes space for storing this pointer

Push EBX

PUSH ESI

Push EDI

Push ECX

Lea Edi, [EBP-44H]

MOV ECX, 11h

Mov Eax, 0cccccccch

Rep Stos DWORD PTR [EDI]

POP ECX

MOV DWORD PTR [EBP-4], ECX

RETURN (A B);

MOV EAX, DWORD PTR [EBP 8]

Add Eax, DWORD PTR [EBP 0CH]

POP EDI

POP ESI

POP EBX

MOV ESP, EBP

POP EBP

RET 8; Qing stack

5, naked attribute

When the four calls described above, the compiler generates code to save the value in ESI, EDI, EBX, EBP registers, generate code recovery of these registers when exiting functions. For functions that define the Naked property, the compiler does not automatically generate such code, and you need to manually use the embedded assembly to control the stack management in the function implementation. Since the Naked property is not a type modifier, it must be used together with the __Declspec. The following code defines a function that uses Naked properties and its implementation:

__DECLSPEC (Naked) Func ()

{

INT I;

Int J;

_asm

{

Push EBPMOV EBP, ESP

SUB ESP, __local_size

}

_asm

{

MOV ESP, EBP

POP EBP

RET

}

}

Naked attributes and this section are not large, please refer to MSDN for details.

6, WinApi

There is also a WINAPI macro that is worth mentioning, which can be translated into an appropriate call agreement for functions. This macro is defined in WINDEF.H. Here is some of the contents in WINDEF.H:

#define cdecl_cdecl

#define WinAPI CDECL

#define callback __stdcall

#define winapi __stdcall

#define apientry WinApi

This shows that WinAPI, Callback, Apientry is likely to function.

2. Name Decoration C or C Function Internal (Compiler and Link) is identified by a modir name. The modulation of the function is a string generated when the compiler is generated when the compile function is defined or the prototype. The compiler is modified to the function name when creating .Obj files. In some cases the name of the function is necessary, if the output is specified in the module definition file, the output C overload function, the constructor, the destructive function, and the C or C functions such as in the assembly code.

In VC , the function modification is determined by the compilation type (C or C ), a function name, a class name, a call agreement, a return type, and a number of factors, and more. The following is divided by C-compilation, C compilation (non-class member function) and C class and its member function compiles: 1. When the function name is modified When the function is used as the __cdecl call, the compiler is only preceded in the original function. The previous underscore prefix, format is _functionName. For example: function int __cdecl add (int A, int b), after outputting: _ADD.

When a function is using the __stdcall call convention, the compiler adds a downline prefix before the original function name, and the number of bytes of @ symbols and function parameters is added, and the format is _functionName @ number. For example: Function INT __STDCALL ADD (INT A, INT B), is: _ADD @ 8 after output.

When the function is in the __fastcall call convention, the compiler plus one @ symbol before the original function name, and the later is the number of bytes plus a @ symbol and function parameters, the format is @ functionname @ number. For example: Function INT __FASTCALL ADD (INT A, INT B), after outputting: @ add @ 8.

The above changes will not change the case in the original function name. Continued: DLL calls and name modifications (1) DLL in DLL is modified (3)

转载请注明原文地址:https://www.9cbs.com/read-26674.html

New Post(0)