Keywords for function definitions in DLL

zhaozj2021-02-16  66

1. EXTERN "C" is used to tell the compiler, name it in c, not to C named method, because C named rule will be decorative words such as parameter type after the letter name, this will Cause other programs such as VC , VB, etc. cannot be used.

2, __ declspec keyword, which can pass two parameters of DLLIMPORT and DLLEXPORT to represent input functions and output functions, respectively. The output function must be used to define the function using __Declspec (DLLEXPORT). Vibrants If you want to use the input function, you must use __DECLSPEC (DLLIMPORT) to define the function.

3, Calling Convention

The calling convention options determine the order in which arguments passed to functions are pushed onto the stack; which function, calling or called, removes the arguments from the stack; and the name-decorating convention that the compiler uses to identify individual functions.

/ Gd__cdeclSpecifies the C calling convention for all functions that are not C member functions or are not marked as __stdcall or __fastcall. This is the default setting. / Gr__fastcallSpecifies the __fastcall calling convention for all functions that are not C member functions or are not marked as __cdecl or __stdcall. All __fastcall functions must have prototypes. / Gz__stdcallSpecifies the __stdcall calling convention for all prototyped C functions that do not take a variable number of arguments and are not marked as __cdecl or __fastcall. All __stdcall functions must have prototypes. _stdcall parameter The stack is in the order of the C language (from right to left), but is different from the C language that it is cleared by the caller to clear the parameters from the stack, so its compilation file is smaller than the _cDecl. _stdcall is the default call agreement in the Windows API function, VB, VFP, etc. also use this agreement. _CDECL is the default call method adopted by the C language. It has the advantage of supporting variable parameter calls such as PrintF. In addition, VC is also different from the name conversion method of the two call methods.

example:

Declaration function

Extern "C" __declspec (dllexport) function type (function parameters);

Implement function:

__DECLSPEC (DLLEXPORT) function type __cdecl function name (function parameters)

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

New Post(0)