Solved

CALLBACK :when should I use this...

Posted on 2004-03-31
7
1,223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 144
Find Visual Studio Tools 2 123
How to split this in C++ 4 119
How to increment counter variables in ANSI  C (Not C# or C++). 9 42
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

733 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