Jump to content
  • Advertisement
Sign in to follow this  
ekrax

never have understood this in a windows program ...

This topic is 5210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
so this is not the same as a linkage specifier?

ie.
extern "C" void My_C_Function ();

sorta thing?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!