Platform SDK: DirectX

Step 1: Enumerate Force-Feedback Devices

[Visual Basic]

The information in this topic pertains only to applications written in C++. See DirectInput Visual Basic Tutorials.

[C++]

The first step is to ensure that a force-feedback device is available on the system. You do this by calling the IDirectInput7::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:

LPDIRECTINPUTDEVICE7  g_lpdid7 = NULL;   
 
lpdi->EnumDevices(DIDEVTYPE_JOYSTICK, 
                  DIEnumDevicesProc,
                  NULL, 
                  DIEDFL_FORCEFEEDBACK | DIEDFL_ATTACHEDONLY);
if (g_lpdid7 == NULL)
  {    
  // No force-feedback joystick available; take appropriate action. 
  }
 

In the example, lpdi is an initialized pointer to the IDirectInput7 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 next in Step 2: Create the DirectInput Force-Feedback Device.