i am learning windows programming lately, i just wanna ask, isn't it that the syntax of a function is type name ( parameter1, parameter2, ...) { statements }?
but when in windows programming you get something like this int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { }
or LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { }
in which i understand in the first example the type is int and the name is WinMain then followed by the parameters.. my question is, what do you call that extra WINAPI/CALLBACK word?
That's a calling convention. It tells the compiler how the function parameters are to be pushed to the stack in the generated assembly.
The details of it are not something you really need to worry about. Just know that if the documentation indicates you should use a specific calling convetion (as you should in the above WinMain and WndProc examples you posted), just do what the documentation says.
see "cdecl" and "stdcall". CALLBACK and WINAPI are probably just #defines of one of those.
EDIT: or are they stanard? Maybe they're compiler extentions? Nah they have to be standard....
Oh well I'm not really sure whether or not they're really in the standard. My guess is yes they are, but I can't give you a 100% answer.
EDIT2: nah it's looking more and more like they are compiler extensions. I guess that's why it's a good idea to #define them instead of using them directly.
it kinda bothers me.. if having callback convention is legal in c++ then every programmer must be able to make their own callback conventions for their own library if ever they are writing one..
if anyone here has the knowledge on how to do this, would you please share it?
if having callback convention is legal in c++ then every programmer must be able to make their own callback conventions for their own library if ever they are writing one..
That's almost kind of why they exist.
It's not frivilous like you're making it sound. People aren't just making new calling conventions for fun. It's just that different languages compile code differently for whatever reasons.
By C/C++ allowing you to specify a calling convention for a function, it lets you use a library (say one that's dynamically linked) that may have been compiled in another language. Without being able to specify this, such foreign libraries might be impossible to use in C/C++
Calling conventions are processor-specific, since they define how the compiler will generate the Assembly to handle communication between stack frames. E.g. Is it the caller's or the callee's job to pop the parameters? How will the return value be sent back? Will the parameters be passed through the stack or some other way (fastcall)?
IIRC, not even where in the function declaration to specify the calling convention is standard. GCC and VC++ use different syntaxes. Although I think GCC also accepts the VC++ syntax, since I can't remember having ever used conditional compilation to specify calling conventions in cross-platform code. I may be wrong, though.
Nothing can stop an Assembly programmer from using, say, the heap to pass parameters, but then no one will be able to call those functions from C/++ because there are no calling conventions that match that.