A callback can be used for notifications. For
instance, you need to set a timer in your application. Each time the
timer expires, your application must be notified. But, the implementer
of the time'rs mechanism doesn't know anything about your application.
It only wants a pointer to a function with a given prototype, and in
using that pointer it makes a callback, notifying your application
about the event that has occurred. Indeed, the SetTimer() WinAPI uses a
callback function to notify that the timer has expired (and, in case
there is no callback function provided, it posts a message to the
application's queue).
Another example from WinAPI functions that use
callback mechanism is EnumWindow(), which enumerates all the top-level
windows on the screen. EnumWindow() iterates over the top-level
windows, calling an application-provided function for each window,
passing the handler of the window. If the callee returns a value, the
iteration continues; otherwise, it stops. EnumWindows() just doesn't
care where the callee is and what it does with the handler it passes
over. It is only interested in the return value, because based on that
it continues its execution or not.
The implementation of these two functions performs a
sorting of the array. But, each time there is a need to decide which of
two elements goes first, a callback is made to the function whose
address was passed as an argument. For the library writer, it doesn't
matter where that function is implemented, or how it is implemented.
All that matters it is that it takes the address of two elements (that
are the two be compared) and it returns one of the following values
(this is a contract between the library developers and its clients):
In the above code, you can see the word __stdcall
in the function's prototype. Because it starts with a double
underscore, it is, of course, a compiler-specific extension, more
exactly a Microsoft-specific one. Any compiler that supports
development of Win32-based applications must support this or an
equivalent one. A function that is marked with __stdcall uses
the standard calling convention so named because all Win32 API
functions (except the few that take variable arguments) use it.
Functions that follow the standard calling convention remove the
parameters from the stack before they return to the caller. This is the
standard convention for Pascal. But in C/C++, the calling convention is
that the caller cleans up the stack instead of the called function. To
enforce that a function uses the C/C++ calling convention, __cdecl must be used. Variable argument functions use the C/C++ calling convention.
.
C++ Methods as Callback Functions
Because you probably write in C++, you want your callback function a method of a class. But, if you try this:
class CCallbackTester
{
public:
int CALLBACK CompareInts(const byte* velem1, const byte* velem2);
};
Bubblesort((byte*)array, 5, sizeof(array[0]),
&CCallbackTester::CompareInts);
with a MS compiler, you get this compilation error:
error C2664: 'Bubblesort' : cannot convert parameter 4 from 'int
(__stdcall CCallbackTester::*)(const unsigned char *,const unsigned
char *)' to 'int (__stdcall *)(const unsigned char *,const unsigned
char *)' There is no context in which this conversion is possible
That happens because non-static member functions have an additional parameter, pointer this (see this for more).
That obliges you to make the member function static. If that's not
acceptable, you can use several techniques to overcome that. Check the
following links to learn more.
Notices
The attached files contain two projects. SortingDLL is a
Win32 DLL project. The sort.dll output library exports the two sorting
functions, Bubblesort() and Quicksort(). The second project, SortDemo, is a Win32 Console Application that demonstrates how to use the sort.dll library. The output directory for both projects is Shared directory, where the following files can be found: sort.h, sort.dll, sort.lib, and SortDemo.exe.
Further References
About the Author
Marius Bancila is a Microsoft MVP
for VC++. He works as a software developer for a Norwegian-based
company. He is mainly focused on building desktop applications with MFC
and VC#. He keeps a blog at www.mariusbancila.ro/blog, focused on
Windows programming. In July 2007 together with two other Romanian MVPs
he created codexpert.ro, a community for Romanian C++/VC++ programmers.
Downloads
- demo sample code