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 IDirectInput::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:

LPDIRECTINPUTDEVICE2  g_lpdid2Game = NULL;   
 
lpdi->EnumDevices(DIDEVTYPE_JOYSTICK, 
                  DIEnumDevicesProc,
                  NULL, 
                  DIEDFL_FORCEFEEDBACK | DIEDFL_ATTACHEDONLY);
if (g_lpdid2Game == NULL)
  {    
  // no force-feedback joystick available; take appropriate action 
  }
 

In the example, lpdi is an initialized pointer to the IDirectInput interface. The first parameter to EnumDevices restricts the enumeration to joystick-type devices. The second parameter is the callback function that's going to be called each time 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's not used. Finally, the flags restrict the enumeration further to devices actually 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's assumed that the first device found is the one you want to use.) You'll do this in Step 2: Creating the DirectInput Force Feedback Device.