Solved

CALLBACK :when should I use this...

Posted on 2004-03-31
7
1,207 Views
Last Modified: 2008-02-01
Hi, what is the function of the word CALLBACK ? Can this be skipped,and, if not, why ???
thanks.
0
Comment
Question by:plinius
  • 3
  • 3
7 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10723622
CALLBACK is defined as __stdcall

MSDN says:

__stdcall
Home |  Overview |  How Do I

Microsoft Specific —>

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl. Functions that use this calling convention require a function prototype. The following list shows the implementation of this calling convention.

Element Implementation
Argument-passing order Right to left.
Argument-passing convention By value, unless a pointer or reference type is passed.
Stack-maintenance responsibility Called function pops its own arguments from the stack.
Name-decoration convention An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows: _func@12
Case-translation convention None


The /Gz compiler option specifies __stdcall for all functions not explicitly declared with a different calling convention.

Functions declared using the __stdcall modifier return values the same way as functions declared using __cdecl.

END Microsoft Specific

-----------------------------------------------------------------------------------

That means, you may omit it if you don't have a variable number of arguments or if the function isn't called by a Non-C and Non-C++ executable.

Regards, Alex
0
 
LVL 4

Expert Comment

by:booki
ID: 10723681
plinius,

>> what is the function of the word CALLBACK

CALLBACK is usually a macro for __stdcall:
    #define CALLBACK __stdcall
It specifies the calling convention of functions.  The default calling convention is __cdecl.  The differences can be seen below.

__stdcall:
    Argument-passing order                           Right to left.
    Argument-passing convention                   By value, unless a pointer or reference type is passed.
    Stack-maintenance responsibility              Called function pops its own arguments from the stack.
    Name-decoration convention                    An underscore (_) is prefixed to the name. The name
                                                                 is followed by the at sign (@) followed by the number
                                                                 of bytes (in decimal) in the argument list. Therefore,
                                                                 the function declared as int func( int a, double b ) is
                                                                 decorated as follows: _func@12

__cdecl:
    Argument-passing order                          Right to left.
    Argument-passing convention                  By value, unless a pointer or reference type is passed.
    Stack-maintenance responsibility              Called function pops its own arguments from the stack.
    Name-decoration convention                    An underscore (_) is prefixed to the name. The name
                                                                 is followed by the at sign (@) followed by the number
                                                                 of bytes (in decimal) in the argument list. Therefore,
                                                                 the function declared as int func( int a, double b ) is
                                                                 decorated as follows: _func@12

>> Can this be skipped,and, if not, why ???

No.  The compiler needs to know the correct calling convention in order to generate correct code.

b.
0
 
LVL 4

Accepted Solution

by:
booki earned 30 total points
ID: 10723737
oops, .. should be

__cdecl:
    Argument-passing order                       Right to left
    Stack-maintenance responsibility           Calling function pops the arguments from the stack
    Name-decoration convention                 Underscore character (_) is prefixed to names
    Case-translation convention                   No case translation performed

b.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10723810
>>>> Can this be skipped,and, if not, why ???

>> No.  The compiler needs to know the correct calling convention in order to generate correct code.

The answer is only right if the callback function is called from an SPU that has a different calling convention. If a function is used for example in a thread and both are using same prototype using or omitting CALLBACK keyword makes no difference.


Regards, Alex
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 30 total points
ID: 10723936
I found this in MSDN:

The calling convention determines how a program makes a call and where the parameters are passed. In a single-language program, calling conventions are nearly always correct, because there is one default for all modules and because header files enforce consistency between the caller and the called routine. In a mixed-language program, different languages cannot share the same header files. It’s easy to link Fortran and C modules that use different calling conventions, and the error isn’t apparent until the bad call is made at run time, causing immediate program failure. Therefore, you should check calling conventions carefully for each mixed-language call.

The following table summarizes how C and Fortran calling conventions work.

C and Fortran Calling Conventions

Calling convention Parameter passing Stack cleared by
C/C++ Pushes parameters on the stack, in reverse order (right to left) Caller
Fortran (__stdcall) Pushes parameters on the stack, in reverse order (right to left) Called function


In C and C++ modules, you can specify the Fortran calling convention by using the __stdcall keyword in a function prototype or definition. The __stdcall convention is also used by window procedures and API functions. For example, the following C language prototype sets up a function call to a subroutine using the Fortran calling convention:

extern void __stdcall fortran_routine (int n);

Instead of changing the calling convention of the C code, you can adjust the Fortran source code by using the C attribute, enclosed in brackets ([ ]). For example, the following declaration assumes the subroutine is called with the C calling convention:

      SUBROUTINE CALLED_FROM_C [C] (A)
      INTEGER*4 A

It should be clear that calling conventions need only agree between individual calls and the called routines, and that the conventions must be the same: Both caller and called routine must use the C/C++ convention or both must use the __stdcall convention (the Fortran default).

Note   In programs written for the graphical user interface of Windows, PASCAL, WINAPI, and CALLBACK are all defined with __stdcall. But the C language default is still cdecl.

The following table summarizes how to specify calling conventions. You can always specify calling conventions explicitly rather than relying on the default, which is a good technique for mixed-language programming.

--------------------------------------------------------------------

As said before, this is needed for mixed-language programming and could be alwayss done for callback functions as it has no disadvantages.

Regards, Alex
0
 
LVL 4

Expert Comment

by:booki
ID: 10724595
itsmeandnobodyelse,

i suppose my post looks like i'm contesting your answer.  i apologize as i didn't see your response when i submitted my comment.  anyway, i believe we are in agreement.  calling conventions (whatever they may be) must match between caller and callee.

typically, you will see that the win32 api requires a function pointer as a parameter.  for example:

BOOL EnumResourceNames(
  HMODULE hModule,             // module handle
  LPCTSTR lpszType,            // resource type
  ENUMRESNAMEPROC lpEnumFunc,  // callback function
  LONG_PTR lParam              // application-defined parameter
);

typedef BOOL (CALLBACK* ENUMRESNAMEPROC)(HMODULE hModule, LPCSTR lpType,
        LPSTR lpName, LONG_PTR lParam);

the function you supply should not omit the CALLBACK (__stdcall).  this is what was meant by no.  if the caller is expecting __stdcall then you must supply a __stdcall function, if the callee is expecting __stdcall then you must call with __stdcall.  it cannot be omitted.

for functions defined and called by you.. do as you wish.

b.
0
 

Author Comment

by:plinius
ID: 10727459
thanks a lot, both. This makes everything more clear.....
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question