never have understood this in a windows program ...

Started by
11 comments, last by Emmanuel Deloget 19 years, 7 months ago
i finally have a grasp on exactly everything you need to code for a basic windows program, however i don't understand the syntax on 2 functions ... the WndProc function ... LRESULT CALLBACK WndProc ( // ... ); and WinMain ... int WINAPI WinMain ... what im confused about is how there looks like there is 2 return types ... this makes no sense to me, LRESULT CALLBACK ... how can you declare a function with 2 types? this has always confused me, what is going on? thanks for any help.
Advertisement
The LRESULT is the return type. CALLBACK defines how to call the function. There are different ways to call functions, like __fastcall, you just use CALLBACK to make sure that the function is called correctly.
to add something here. this is called a "calling convention". because some languages pass the parameters of a function through the stack in a different order, this forces the compiler to pass the arguments in a given order. this is needed sometimes for functions used by different languages.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
LRESULT is a macro for the return type, unsigned long I believe. CALLBACK and WINAPI is macros for the calling-convetion, i.e. the way you want the compiler to push function arguments on the stack and handle return values when producing the assembler code.

//edit: meh, gotta stop keeping pages in a background tab for half an hour before answering them =P
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
so this is not the same as a linkage specifier?

ie.
extern "C" void My_C_Function ();

sorta thing?
the extern "C" has to do with name mangling... More specifically, to make the names of c++ functions to be the same name a c compiler would expect.
Quote:Original post by ekrax
... how can you declare a function with 2 types? ...


2 "types"? That's nothing. How about 4 "types"?
    static const MyClass __cdecl foo(); 
As you can see they aren't all types. LRESULT, CALLBACK, and WINAPI are macros and aren't necessarily types either.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
well i know those aren't types, i just didn't know there was another "setting" you could apply to defining variables or functions, to me i thought something like this ...

mutable volatile int MyFunc ();
or something like ...
static const float MyFunc ();

was the most qualifers or specifers you could apply i didn't know that you could add a "calling convention" to it ... i have been reading "c++ from the ground up" by herbet schildt, but i guess this subject isn't covered in this book. so this brings me to belive that using a calling convention is very rare?

so anyways are there are built in calling conventions? or must you define them yourself using "C" style macros or asm or something else?
Basically, there are default calling conventions, and it's quite rare that you need to specify one. They specify the order in which things go on the stack, how the register values are saved (by caller or callee), and other stuff like that, which you're trying to avoid worrying about in the first place by not writing in assembler.

Common ones in C++ include _cdecl (normal, "free function"), _thiscall (used for member functions, and specifies to put the 'this' pointer on the stack in addition to the other arguments), _fastcall (Microsoft-specific I think) and _dllimport/_dllexport (for DLLs, also MS-specific). I think I got that right. Anyway, this stuff is *normally* taken care of for you (e.g. you don't need to write the _thiscall for your member functions). But when you are writing a DLL, or setting up WinMain, you need to worry about it.
A little correction, the fastcall convention is not microsoft specific, but the thiscall one is. Check them up in the msdn if you want to know more about them. And as already stated, you rarely need to specific which calling convention to use, it's mainly used when sharing code between compilers/languages.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams

This topic is closed to new replies.

Advertisement