Platform SDK: DirectX

Step 5: Gain Access to the Joystick

[Visual Basic]

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

[C++]

After your application sets a joystick's behavior, it can acquire access to the device by calling the IDirectInputDevice7::Acquire method. The application must acquire the device before retrieving data from it. The Acquire method accepts no parameters.

The Space Donuts application takes care of acquisition in the ReacquireInput function. This function does double duty, serving both to acquire the device on startup and to reacquire it if for some reason a DIERR_INPUTLOST error is returned when the application tries to get input data.

In the following code, g_pdevCurrent is a global pointer to whatever DirectInput device is currently in use.

BOOL ReacquireInput(void) 
{ 
    HRESULT hRes; 
 
    // if we have a current device 
    if (g_pdevCurrent) 
    { 
        // acquire the device 
        hRes = IDirectInputDevice_Acquire(g_pdevCurrent); 
        // The call above is a macro that expands to: 
        // g_pdevCurrent->lpVtbl->Acquire(g_pdevCurrent); 
 
        if (SUCCEEDED(hRes)) 
        { 
            // acquisition successful 
            return TRUE; 
        } 
        else 
        { 
            // acquisition failed 
            return FALSE; 
        } 
    } 
    else 
    { 
        // we don't have a current device 
        return FALSE; 
    } 
} 
 

In this example, acquisition is effected by a call to IDirectInputDevice7_Acquire, a macro defined in Dinput.h that simplifies the C call to the IDirectInputDevice7::Acquire method.

Next: Step 6: Retrieve Data from the Joystick