Microsoft DirectX 9.0

Step 2: Creating the DirectInput Joystick Device

After creating the Microsoft?DirectInput?object, the application must retrieve a pointer to an IDirectInputDevice8 interface, which is used to perform most joystick-related tasks. In the Joystick Sample, this is done in the callback function EnumJoysticksCallback, which is called each time a joystick is enumerated.

The following sample code is the first part of the callback function.

BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE*     
                                       pdidInstance, VOID* pContext)
    HRESULT hr;

    // Obtain an interface to the enumerated joystick.
    hr = g_pDI->CreateDevice(pdidInstance->guidInstance,  
                                &g_pJoystick, NULL);
        return DIENUM_CONTINUE;

    return DIENUM_STOP;

The EnumJoysticksCallback callback function parameters are as follows:

The first task of the callback function is to create the device. The IDirectInput8::CreateDevice method accepts three parameters.

The first 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 second parameter is the address of the variable that is initialized with a valid interface pointer if the call succeeds.

The third parameter specifies the address of the controlling object's IUnknown for use in COM aggregation. The Joystick Sample does not use aggregation, so the parameter is NULL.

Note that if 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.

After creating the DirectInput joystick device, go to Step 3: Setting the Joystick Data Format.

© 2002 Microsoft Corporation. All rights reserved.