Solved

CALLBACK :when should I use this...

Posted on 2004-03-31
7
1,196 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks a lot, both. This makes everything more clear.....
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

6 Experts available now in Live!

Get 1:1 Help Now