what is a CALLBACK

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

what is actually the use and meaning when lets say you specify CALLBACK to a function some say that its for calling Convention, some say its when using function pointers im confused pls advise

Share on other sites
Just for reference, because I didn't realize this at first, he's talking about the C macro CALLBACK not what callbacks are (i think)

and I have no freaking clue, sounds like a win32 thing.

Share on other sites
// straight from the WinDef.h:#ifdef _MAC#define CALLBACK    PASCAL#define WINAPI      CDECL#define WINAPIV     CDECL#define APIENTRY    WINAPI#define APIPRIVATE  CDECL#ifdef _68K_#define PASCAL      __pascal#else#define PASCAL#endif#elif (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)#define CALLBACK    __stdcall#define WINAPI      __stdcall#define WINAPIV     __cdecl#define APIENTRY    WINAPI#define APIPRIVATE  __stdcall#define PASCAL      __stdcall#else#define CALLBACK#define WINAPI#define WINAPIV#define APIENTRY    WINAPI#define APIPRIVATE#define PASCAL      pascal#endif

In essence on most wi32 platforms CALLBCK is just __stdcall.

Share on other sites
Quote:
 From MSDN:Microsoft SpecificThe __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.return-type __stdcall function-name[(argument-list)]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

Formatting...
[/edit]

Share on other sites
I'm not sure about a true definition but the way I see it is that callback is usually used for functions. Many API's including the Windows API will use these functions when a certain event occures: in other words, it will 'callback' to them. The Windows API however, is usually more focuesd on using messages than using callback functions. Often, the function itself will be passed as a parameter and the API being used will call the function passed to it when a certain event occures or a certain condition or set of conditions is true.

This is only my interpretation of what a callback is used for and it could be wrong.

Share on other sites
Quote:
 Original post by yuppieswhat is actually the use and meaning when lets say you specify CALLBACK to a functionsome say that its for calling Convention, some say its when using function pointersim confused pls advise

To answer your question directly: both is right. CALLBACK is used to define a common calling convention, esp. to avoid stack corruption when using funktion pointers. Without a common calling convention within the API (which is sperad across many DLLs), name lookup as well as function arguement retrieval and stack constistency could be messed up when DLL A calls code from DLL B (be it a function call from within the API or a function pointer provided by a user program).

Since Microsoft introduced other calling convention, such as __fastcall, which passes as many parameters using registers as possible, there had to be a common standard in order to keep the stack usage and argument retrievial consistent between multiple processes.

Hope that help,
Pat.