What is better to declare for calling convention of Windows program?

624 views Asked by At

I have read an article about Calling convention (__stdcall, __fastcall, pascal, cdecl etc...)

And I wonder: Why is often __stdcall (WinAPI) declared for WinMain() function?

As I read, __stdcall operates with the stack, __fastcall operates on registers and not using stack at all.

So , I've tried to declare WinMain() with the __fastcall. Compiler (Visual C++) has given me an error.

error C2373: 'WinMain' : redefinition; different type modifiers
c:\program files\microsoft sdks\windows\v6.0a\include\winbase.h(2560) : see declaration of 'WinMain'

Why can't I use __fastcall for WinMain() and is there any possibility to use it for it?

Because to work on registers without using stack is miles faster, isn't it?

PS My suggestion is, that there are some method contracts that disallow me to use __fastcall, but it's only my suggestion.

2

There are 2 answers

0
engf-010 On BEST ANSWER

You can only specify the calling convention on functions that you write and/or you have the source code of. You cannot change calling conventions of function that are in a library (static/dynamic) since those are already compiled/linked. Important is that the declaration and definition have the same convention.

BTW : you wouldn't gain anything by having (win-)main having the fastcall convention since it's only called once! You would consider fastcall on functions with many small parameters (that fit in registers) that are called very very often during long periodes of time.

The (buildin) startup routine for windows programs will call either WinMain or main (depending on GUI or Console app) with a specific convention. If you write a WinMain or main with a different convention then the linker will complain.

0
Igor Tandetnik On

WinMain must be __stdcall. It's called by the CRT start-up code, which is already built to pass parameters in a way defined by __stdcall convention.