__stdcall help

This is a discussion on __stdcall help within the Windows Programming forums, part of the Platform Specific Boards category; Hi Every One, I have moved into Windows32 programming and have been doing some reading on __stdcall calling conventions, The ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    __stdcall help

    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.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    It's nothing you should concern yourself about.

    C/C++ use __cdecl by default, which means that the passed arguments are cleaned up by the caller.

    __stdcall tells C/C++ that the arguments will be cleaned by the called one. That's all.

    ( Usually DLLs/shared_libraries use that calling convertion )
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by grneyeddvl View Post
    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.
    Last edited by Steve A.; 03-28-2011 at 03:25 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grneyeddvl View Post
    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.
    __stdcall is the default calling convention, used by windows. It dictates how the function handles the allocation of stack space and the cleanup as a function exits. It has no effect on how you call the function... just use somefunction(a,b,c,d); like always.

    Other calling conventions are:
    __cdecl the native C calling convention used by CLI (console) programs.
    __fastcall which is used by some ASM programs.

    Quote Originally Posted by Steve A. View Post
    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.
    Actually... prototypes are only required in 2 situations...
    1) Where you cannot order your function definitions so that the definition is seen before it is called.

    2) In header files reporting functions across translation units (source files).

    Code:
    // no prototypes required
    
    int multiply(int a, int b)
      { return a * b; }
    
    int main (void)
      { int c;
         c = multiply (3,6);
        return 0; }
    Last edited by CommonTater; 03-28-2011 at 11:44 PM.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by CommonTater View Post
    __stdcall is the default calling convention, used by windows.
    The Windows APIs are all __stdcall, but Visual Studio actually uses __cdecl as the default, probably for interoperability with other compilers.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brewbuck View Post
    The Windows APIs are all __stdcall, but Visual Studio actually uses __cdecl as the default, probably for interoperability with other compilers.
    Hmmm... didn't know that, thanks. Leave it to Microsoft to do something weird.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Thanks all for your replies i'm new to the Advanced side of c++ (i.e) pointer functions etc, reading all of your posts as helped me better understand what is going on Thanks alot guys.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by CommonTater View Post
    Hmmm... didn't know that, thanks. Leave it to Microsoft to do something weird.
    I think it's not a bad compromise. __stdcall for the OS, since it usually produces smaller code, and __cdecl for things that need to interoperate with code compiled by other compilers. You can control all of this if you want, but the point is for the defaults to be as unsurprising as possible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21