0

I wanted to start learning the windows api, so I looked at some tutorials, and instantly I noticed that the main function appeared to return multiple values.

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

What does all of this mean? Shouldn't it be int WinMain or WINAPI WinMain but not both?

  • @Claudiu oh im new here i didnt know questions were actually saved that long – user3466304 Mar 27 '14 at 21:12
  • @Claudiu actually those answers on that question didn't answer what i was asking – user3466304 Mar 27 '14 at 21:15
  • It's just a macro. It's equivalent to `int __stdcall WinMain(...`. It's a compiler-specific extension, not a standard. The answer to your question is that it doesn't return two values, since `WINAPI` isn't a return value. – Claudiu Mar 27 '14 at 21:27

2 Answers2

5

WINAPI is a macro that expands to __stdcall. It specifies the calling convention, not the return type. The return type is still 'int'

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • Im still confused with the placement of it – user3466304 Mar 27 '14 at 21:16
  • Just regard it as a non-standard C++ extension for targeting Windows platforms. Microsoft could have placed it anywhere they liked so long as it didn't completely ruin standard C++ grammar. – Bathsheba Mar 27 '14 at 21:17
  • @Bathsheba ok so i dont have to worry about that wierd syntax :) – user3466304 Mar 27 '14 at 21:18
  • 1
    Bathsheba is correct about the placement - it's just compiler convention. I'd disagree slightly about the 'daft'. Any function caller and callee have to agree on how arguments are passed. The most general way -`cdecl` - is not always the fastest way, especially on the old intel segmented architectures. So the microsoft API supported some faster alternatives. See [Raymond Chen](http://blogs.msdn.com/b/oldnewthing/archive/2004/01/02/47184.aspx) for some background. – AShelly Mar 27 '14 at 21:25
0

In order for C++ compilers to target Windows efficiently, some extensions to standard C++ were made.

One of them is WINAPI, which in Win32, gets expanded to __stdcall. That's a non-standard compiler directive that tells the compiler to pass arguments from left to right (for compatibility with VBA etc). Technically, it's called a calling convention.

Hence it has nothing to do with the return type and therefore you still need to write an explicit return type.

(Note that in Win64, Microsoft dispense with __stdcall entirely so WINAPI is defined to blank).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483