Microsoft DirectX 8.0 (C++)

Implementing a Callback Function

Because callback functions have different purposes and usage, they are documented in the appropriate Reference section in much the same way as a regular function. However, a callback function reference is essentially a template that describes how to implement the function, not a normal API reference. The callback function reference provides the following information:

You should declare the function as a CALLBACK or WINAPI type. Either type is acceptable. You can use any function name that you wish. The name used in the documentation is simply a convenient label for a particular callback function.

Implement the function according to the description in the reference. The implementation details will depend on the particular function and the requirements of your application. See the sample code for some examples of how to implement various callback functions.

Pass a pointer to the function to the appropriate Microsoft® DirectX® component. The DirectX component can then use the function pointer to call the function. The way in which you pass this pointer varies from function to function, so you should see the particular function's reference for details.

The following code fragment is borrowed from the Microsoft DirectInput® Joystick sample. It sketches out the essential elements of a DIEnumDeviceObjectsCallback implementation that is used to enumerate the axes of a joystick.

//The function declaration
BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,
                                VOID* pContext );
...
//Pass the function pointer to DirectInput by calling the 
//IDirectInputDevice8::EnumObjects method
if ( FAILED( hr = g_pJoystick->EnumObjects( EnumAxesCallback, 
                                           (VOID*)hDlg,
                                            DIDFT_AXIS )));
...
//The function implementation
BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,
                                VOID* pContext )
{
//Process the information passed in through the two parameters
//Return DIENUM_CONTINUE to request the next device object
//Return DIENUM_STOP to stop the enumeration
}