Step 1: Enumerating Force-Feedback Devices

The first step is to ensure that a force-feedback device is available on the system. You do this by calling the IDirectInput8::EnumDevices method. In the following example, the global pointer to the game device interface is initialized only if the enumeration has succeeded in finding at least one suitable device:

LPDIRECTINPUTDEVICE8  g_lpDIDevice = NULL;   

hr = g_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL, 
                         DIEnumDevicesProc,
                         NULL, 
                         DIEDFL_FORCEFEEDBACK | DIEDFL_ATTACHEDONLY);
if (FAILED(hr))
{    
  // No force-feedback joystick available; take appropriate action. 
}

In the example, g_lpDI is an initialized pointer to the IDirectInput8 Interface interface. The first parameter to IDirectInput8::EnumDevices restricts the enumeration to joystick-type devices. The second parameter is the callback function that is called whenever DirectInput identifies a device that qualifies for enumeration. The third parameter is for user-defined data to be passed in or out of the callback function; in this case it is not used. Finally, the flags further restrict the enumeration to devices attached to the system that support force feedback.

The callback function is a convenient place to initialize the device as soon as it has been found. (It is assumed that the first device found is the one you want to use.) You do this in Step 2: Creating the DirectInput Force-Feedback Device.