What is the point of defining a calling convention?

后端 未结 5 1495
醉话见心
醉话见心 2021-01-24 06:20

For example:

int WINAPI WinMain ( HINSTANCE instance, HINSTANCE prev_instance, PSTR cmd_line, int cmd_show )

WINAPI is a a define that looks li

相关标签:
5条回答
  • 2021-01-24 06:59

    This was originally done during the switchover from 16-bit to 32-bit code. In the 16-bit version of <windows.h> it was:

    #define WINAPI __pascal
    

    WINAPI let you compile for either without modifying the source code. Of course, 16-bit Windows is no longer a factor (at least for most people), but it's still not worth changing all the source code to use __stdcall directly (especially since it could change again someday).

    0 讨论(0)
  • 2021-01-24 07:02

    Because the WINAPI calling convention is not guaranteed to be __stdcall. Code that uses WINAPI will still be correct even when it isn't.

    You can write the function as in your latter example, and it'd work fine - it's just not good practice and would not be portable to a platform where the calling convention is something else.

    0 讨论(0)
  • 2021-01-24 07:04

    Also, back in the day, some Windows libraries used Pascal calling convention and other libraries used C convention. A preprocessor define helped gloss over that.

    0 讨论(0)
  • 2021-01-24 07:09

    You can just write __stdcall in its place, but don't. They've seen fit to #define WINAPI to __stdcall to make that opaque, which is just good programming practice.

    0 讨论(0)
  • 2021-01-24 07:10

    Because WINAPI is a macro (well a #define anyway) it can be "pre-processed" to mean something else or even nothing at all.

    That means you can write more portable code, as you can put in WINAPI when it is required by Win32 to mean __stdcall but, or if it is required in another environment to mean something else or nothing.

    0 讨论(0)
提交回复
热议问题