Solved

CALLBACK :when should I use this...

Posted on 2004-03-31
7
1,203 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now