Step 5: Gaining Access to the Joystick

After your application sets a joystick's behavior, it can acquire access to the device by calling the IDirectInputDevice::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 IDirectInputDevice_Acquire, a macro defined in Dinput.h that simplifies the C call to the IDirectInputDevice::Acquire method.