Thread: WINAPI, WINUSERAPI, and calling conventions...what the heck are these?!

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    6

    WINAPI, WINUSERAPI, and calling conventions...what the heck are these?!

    Hi all--

    I some programming in college using mostly C++ (and some Java) and we learned stuff like quick sorts, hash tables, red-black trees etc. The extent of input/output was cout << "Hello, World!"

    I wanted to learn how to program windows and I searched the internet and everyone said "GET PETZOLD!" So I bought "programming windows 5th addition" (I think it's going out of print...should I be worried ??)

    Anyways Petzold said I needed to be familiar with C programming...since I knew printf() and malloc() existed but had no idea how to use them, I got the K&R book and went through it.

    NOW, I'm into chapter one of petzold (finally) and the hello world function begins with...
    Code:
    int WINAPI WinMain(HINSTANCE hInstance...){...}
    which seemed very odd because nowhere in my c++ or c from k&r did functions ever have more than a return type followed by the function name and parameters in their declaration/definition.

    Petzold just says it's a "calling convention" and determines how a function is to be placed on the stack. Expanding WINAPI to __stdcall. Okay...but then LATER in Chapter 2 he defines the true message box with the following:
    Code:
    WINUSERAPI int WINAPI MessageBoxA(HWND hWnd...){...}
    Now this function has 2 calling conventions (WINAPI and WINUSERAPI)? How many can a function have? what are these exactly? does anyone know of a place somewhere that has a beginner's tutorial for these calling conventions? perusing the index...I cannot find any more information on either of these.

    Any help would be appreciated!
    Thanks!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can ignore the WINUSERAPI. It is ultimately defined (on some compilers) to declspec(dllimport), which is a compiler specific instruction that can give a slight performance boost to DLL functions. When calling a function, you can also ignore the calling convention. The only time you need to remember a calling convention is when you declare a callback function (such as WinMain). In this case, Win32 functions expect the callback function to use the WINAPI (or __stdcall) calling convention. The default calling convention is __cdecl. Even if you forget, your compiler will probably give you a warning. You can find a technical overview of __stdcall and __cdecl here.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    Thanks anonytmouse!

    those links were interesting. It was also nice to know I can move on with learning such great tasks as opening a window or outputting a character without feeling totally lost =)

Popular pages Recent additions subscribe to a feed