Originally Posted by
grneyeddvl
Hi Every One,
I have moved into Windows32 programming and have been doing some reading on __stdcall calling conventions, The problem is i'm not sure i'm fully understanding what is going on when the function like this is declared
The Function Prototype
Code:
HRESULT __stdcall somefunction(parameter1,parameter2,parameter3,parameter4) ;
If i'm correct in understand that this function is called by the windows operating system, and the call orders the parameters from right to left and where to parameters for this function are stored in memory, what i'm have problems with how does windows know when to call this function?.
and where does the return value (HRESULT) go when the function returns.
Thanks everyone for reading and any help would very much appreciated
James.
Hello James,
in C/C++ there are two basic calling conventions in use:
__stdcall = windows API
__cdecl = standard C/C++
One difference between __stdcall and __cdecl calling conventions has to do with who "pops" the stack at the end of a function, on return.
In windows API functions, the called function restores the stack to it's proper order.
In a "standard C" function, the caller (the program calling the function is responsibe for aligning the stack on return.
Generally, a C programmer doesn't have to mess with aligning the stack, the compiler handles all of that.
Assembly Language programmers always have to do stack maintainance.
There are only rare circumstances where C programmer has to maintain the stack. You would have to be doing something out of the ordinary, (where windows is concerned).
Regarding the question about the code sample you posted above, it looks like that is just an example, (copied from somewhere), that simply illustrates a function prototype.
You ask:
what i'm have problems with how does windows know when to call this function?.
well, windows doesn't know when to call it , unless you specifically tell windows to call it in your program.
I take it that you are not only new to win-32, but, new to C/C++ programming as well.
In C/C++, each and every function that you (the programmer) writes must be preceded by a "function prototype".
The "prototype" is for the compiler's benefit. It instructs the compiler what that function is supposed to look like, before it encounters it.
and where does the return value (HRESULT) go when the function returns.
Again, simply part of the prototype.
The return value could be any data type: int, double, char, void, ....etc.
Too many to list here.
HRESULT is merely an example.
Like this:
Code:
int MyFunction(int p1, p2, p3); // prototype
int main()
{
int hResult, param1, param2, param3; //interger variables
hResult = MyFunction(param1, param2, param3); // call function
... // do something with hResult ..
return 0; // done
}
Notice, I did not have to specify any type of calling convention. The compiler handles all of that for me.
IMHO, unless you are doing Assembly Language programming, or some really advanced C programming, you don't need to be overly concerned about the calling conventions used.
Let the compiler handle it.