Step 1: Creating the DirectInput Mouse Device

After creating the DirectInput object, your application should retrieve a pointer to an IDirectInputDevice interface, which will be used to perform most mouse-related tasks. To do this, call the IDirectInput::CreateDevice method.

The CreateDevice method accepts three parameters.

The first parameter is the globally unique identifier (GUID) for the device your application is creating. In this case, since the system mouse will be used, your application should pass the predefined global variable GUID_SysMouse.

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

The third parameter specifies the address of the controlling object's IUnknown interface for use in COM aggregation. Your application probably won't be using aggregation, in which case the parameter will be NULL.

The following sample from Scrawl.cpp attempts to retrieve a pointer to an IDirectInputDevice interface. If the call fails, an error message is displayed and FALSE is returned.

// LPDIRECTINPUT    g_pdi;    // This has been initialized
LPDIRECTINPUTDEVICE g_pMouse; 
HRESULT             hr; 
 
hr = g_pdi->CreateDevice(GUID_SysMouse, &g_pMouse, NULL);
 
if (FAILED(hr)) {
    Complain(hwnd, hr, "CreateDevice(SysMouse)");
    return FALSE;
}