Microsoft DirectX 9.0

Using Callback Functions

A callback function is essentially an event handler that is implemented by an application and called by the system. Microsoft?Windows?applications typically implement multiple callback functions, each one designed for a particular set of events. When an event occurs, the system notifies the application by calling the appropriate callback function. The callback function also usually has a parameter list that the system can use to pass the application more detailed information about the event. The most common example of a callback function is the window procedure. This function is used by the system to pass Windows messages to the applications that owns the window.

Microsoft DirectX?uses callback functions for a variety of purposes. For example, your system supports multiple devices. Microsoft DirectInput?represents each device by a device object, that contains the details of the device's capabilities. Your application will typically need to enumerate the available devices and to examine the device objects in order to handle user input properly. To do this enumeration, you must implement a DIEnumDeviceObjectsCallback callback function.

You start the enumeration process by calling IDirectInputDevice8::EnumObjects and passing the method a pointer to your DIEnumDeviceObjectsCallback callback function. The system will call this function once for each device, and it will pass in a DIDEVICEOBJECTINSTANCE structure containing information about the device's capabilities. After your callback function has processed the information, it can return DIENUM_CONTINUE to request the next device object, or DIENUM_STOP to stop the enumeration.

DirectX uses a number of other callback functions for a variety of purposes. For details, see the documentation for the particular DirectX component.

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 application programming interface (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. The name used in the documentation is just 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 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 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
                                VOID* pContext );
// Pass the function pointer to DirectInput by calling the 
// IDirectInputDevice9::EnumObjects method

if ( FAILED( hr = g_pJoystick->EnumObjects(EnumAxesCallback, 
                                            DIDFT_AXIS )));


// The function implementation
                                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

© 2002 Microsoft Corporation. All rights reserved.