Search code examples
c++cwinapidllcalling-convention

WINAPI identifiers in function declaration C++ in DLL entrypoint function


I'm just learning how to create a dll with C++.

There appears this :

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)

And I can not understand what is "WINAPI" in DllMain()?

I know that a function is :

typeReturn functionName (params) { function body }

typeReturn : is the value that function returns,
functionName : is the name of the function,
params : are the parameters for the function,
{function body} : is the code inside in the function.
...

Then, following the explanation, what does WINAPI mean in C++ that or __stdcall?

I'm not asking what means WINAPI itself.

************ UPDATE **************

C++ has (calling conventions) that is used to put in memory each parameter given in a special way. please read correctly the question and avoid mark it as duplicate, because people learning c/c++ needs learn without fall into confusions


Solution

  • WINAPI is defined as __stdcall.

    Actually __stdcall is a calling convention And different calling conventions push parameters in different ways, Bellow are some of c/c++ Calling Conventions :

    In x86 :

    • C calling convention (__cdecl). The main characteristics of __cdecl calling convention are :

      1. Arguments are passed from right to left, and placed on the stack.
      2. Stack cleanup is performed by the caller.
      3. Function name is decorated by prefixing it with an underscore character '_' .
    • Standard calling convention (__stdcall). The main characteristics of __stdcall calling convention are :

      1. Arguments are passed from right to left, and placed on the stack.
      2. Stack cleanup is performed by the called function.
      3. Function name is decorated by prepending an underscore character and appending a '@' character and the number of bytes of stack space required.
    • Fast calling convention (__fastcall). he main characteristics of __fastcall calling convention are :

      1. The first two function arguments that require 32 bits or less are placed into registers ECX and EDX. The rest of them are pushed on the stack from right to left.
      2. Arguments are popped from the stack by the called function.
      3. Function name is decorated by by prepending a '@' character and appending a '@' and the number of bytes (decimal) of space required by the arguments.

    Consider to Read This Link


    In x64 : In x64, only __fastcall exists. All other attributes are ignored.

    The x64 Application Binary Interface (ABI) uses a four register fast-call calling convention by default.


    Notice : When you call a function, what happens at the assembly level is all the passed-in parameters are pushed to the stack or placed in registers or placed in static storage, then the program jumps to a different area of code. The new area of code looks at the stack and expects the parameters to be placed there.

    Different calling conventions push parameters in different ways. Some might push the first parameter first, or some might push the first param last. Or some might keep a parameter in a register and not push it at all.

    By specifying a calling convention, you are telling the compiler how the parameters are to be pushed.