Platform SDK: DirectX

Step 2: Create the DirectInput Joystick Device

[Visual Basic]

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

[C++]

After creating the DirectInput object, the application must retrieve a pointer to an IDirectInputDevice7 interface, which will be used to perform most joystick-related tasks. In the Space Donuts sample, this is done in the callback function InitJoystickInput, which is called each time a joystick is enumerated.

Here is the first part of the callback function:

BOOL FAR PASCAL InitJoystickInput(LPCDIDEVICEINSTANCE pdinst, 
                                  LPVOID pvRef) 
{ 
   LPDIRECTINPUT7 pdi = pvRef; 
   LPDIRECTINPUTDEVICE7 pdev; 
 
   // Create the DirectInput joystick device. 
   if (pdi->lpVtbl->CreateDeviceEx(pdi, &pdinst->guidInstance, 
          IID_IDirectInputDevice7, (void**)&pdev, NULL) != DI_OK) 
   { 
      OutputDebugString("IDirectInput7::CreateDeviceEx FAILED\n"); 
      return DIENUM_CONTINUE; 
   } 
 

The parameters to the callback function InitJoystickInput are:

The InitJoystickInput sample function declares a local pointer to the DirectInput object, pdi, and assigns it the value passed into the callback. It also declares a local pointer to a DirectInput device, pdev, which is initialized when the device is created.

The first task of the callback function, then, is to create the device. The IDirectInput7::CreateDeviceEx method accepts four parameters.

The first, unnecessary in C++, is a this pointer to the calling DirectInput interface.

The second parameter is a reference to the globally unique identifier (GUID) for the instance of the device. In this case, the GUID is taken from the DIDEVICEINSTANCE structure supplied by DirectInput when it enumerated the device.

The third parameter is the address of the variable that will be initialized with a valid interface pointer if the call succeeds.

The fourth parameter specifies the address of the controlling object's IUnknown interface for use in COM aggregation. Space Donuts doesn't use aggregation, so the parameter is NULL.

Note that if for some reason the device interface cannot be created, DIENUM_CONTINUE is returned from the callback function. This flag instructs DirectInput to keep enumerating as long as there are devices to be enumerated.

Next: Step 3: Set the Joystick Data Format