• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1279
  • Last Modified:

CALLBACK :when should I use this...

Hi, what is the function of the word CALLBACK ? Can this be skipped,and, if not, why ???
thanks.
0
plinius
Asked:
plinius
  • 3
  • 3
2 Solutions
 
itsmeandnobodyelseCommented:
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
 
bookiCommented:
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
 
bookiCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
itsmeandnobodyelseCommented:
>>>> 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
 
itsmeandnobodyelseCommented:
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
 
bookiCommented:
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
 
pliniusAuthor Commented:
thanks a lot, both. This makes everything more clear.....
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now