0

I am reading about Win32 programming with C/C++ and came across a page which defines the WinMain as:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);

I understand most of this, except I do not understand where did the part WINAPI come from?

I know that this is a macro for a calling convention. That's not what I am seeking clarity on. My question is not on calling conventions.

When I look at Microsoft's documentation on C++ functions, and I read through optional parts of a function declaration, I don't see any mention of including a calling convention at any place in the function declaration. So where exactly does Microsoft in their documentation speak about including calling conventions in a function declaration?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user1720897
  • 1,216
  • 3
  • 12
  • 27
  • 3
    https://learn.microsoft.com/en-us/cpp/cpp/calling-example-function-prototype-and-call – RbMm Feb 01 '18 at 07:10
  • 1
    I don't understand the question at all. Surely you can do a websearch to find this information. – David Heffernan Feb 01 '18 at 07:22
  • @DavidHeffernan The question is: if `int` is the return type, `wWinMain` is the function name, the stuff inside the parenthesis are parameters, then what is WINAPI in the function declaration. I thought a function declaration had only a return type, a name and parameters. – user1720897 Feb 01 '18 at 08:12
  • 1
    First of all fine out what the WINAPI macro expands to. Then do a websearch on that. – David Heffernan Feb 01 '18 at 08:19
  • @DavidHeffernan I have explained in the question that I do know what WINAPI expands to. But I am asking what is its place in the function declaration. The question is not about WINAPI, its about its place infunction declarations. Also, a lot of search was done before posting the question here. Please don't make it sound like I have not done a basic search. – user1720897 Feb 01 '18 at 08:39
  • Did you search for what `WINAPI` expands to? Which as you know is `__stdcall`. – David Heffernan Feb 01 '18 at 08:53
  • @DavidHeffernan I did. I KNOW what _stdcall is. I am just asking what is it's place in the function declaration. If I ask you what is `int` in `int fname(void);`, you would say it's the return type. Similarly what in the world is `__stdcall`. – user1720897 Feb 01 '18 at 09:14
  • 1
    `__stdcall` is the calling convention. See https://stackoverflow.com/questions/297654/what-is-stdcall – David Heffernan Feb 01 '18 at 09:40

1 Answers1

2

The portion of Microsoft's documentation that you linked to refers to only standard components of the C++ language. Calling conventions are not part of the C++ specification.

The C++ specification describes how a function can declare its return type and parameters, but it does not define how those values are actually passed between caller and callee. Calling conventions dictate that, and different compilers/platforms implement calling conventions in their own ways. So the C++ specification doesn't describe calling conventions.

In Microsoft's documentation, calling conventions are referred to as Microsoft-Specific Modifiers to the C++ language. Which is technically correct, as any identifier that begins with 1-2 underscores in its name is a vendor-specific extension, and all of the known calling conventions begin with underscores in their names, eg:

__cdecl
__stdcall
__fastcall
__thiscall
__safecall
__vectorcall
__pascal
__fortran
__syscall
etc...

Macros like WINAPI, STDMETHODCALL, etc simply map to a specific calling convention (usually __stdcall, but sometimes __cdecl).

If omitted in a function declaration, the compiler decides which calling convention it wants to use (usually __cdecl).

Compilers from different vendors are not required to implement each others extensions. However, in the case of calling conventions, most compilers at least implement __cdecl and __stdcall, and agree on how they should work, for code portability. But make no mistake, calling conventions are still a vendor-specific extension to the standard language specification.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770