src/joystick/windows/SDL_dxjoystick.c
changeset 5591 17beb16fa838
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
     1.1 --- a/src/joystick/windows/SDL_dxjoystick.c	Thu Aug 04 01:07:13 2011 -0400
     1.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Wed Aug 03 04:22:47 2011 -0400
     1.3 @@ -57,6 +57,7 @@
     1.4  
     1.5  
     1.6  /* local variables */
     1.7 +static SDL_bool coinitialized = SDL_FALSE;
     1.8  static LPDIRECTINPUT dinput = NULL;
     1.9  extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
    1.10                                         LPDIRECTINPUT * ppDI,
    1.11 @@ -284,16 +285,19 @@
    1.12  
    1.13      SYS_NumJoysticks = 0;
    1.14  
    1.15 -    result = CoInitialize(NULL);
    1.16 +    result = WIN_CoInitialize();
    1.17      if (FAILED(result)) {
    1.18          SetDIerror("CoInitialize", result);
    1.19          return (-1);
    1.20      }
    1.21  
    1.22 +    coinitialized = SDL_TRUE;
    1.23 +
    1.24      result = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER,
    1.25                                &IID_IDirectInput, (LPVOID)&dinput);
    1.26  
    1.27      if (FAILED(result)) {
    1.28 +        SDL_SYS_JoystickQuit();
    1.29          SetDIerror("CoCreateInstance", result);
    1.30          return (-1);
    1.31      }
    1.32 @@ -301,6 +305,7 @@
    1.33      /* Because we used CoCreateInstance, we need to Initialize it, first. */
    1.34      instance = GetModuleHandle(NULL);
    1.35      if (instance == NULL) {
    1.36 +        SDL_SYS_JoystickQuit();
    1.37          SDL_SetError("GetModuleHandle() failed with error code %d.",
    1.38                       GetLastError());
    1.39          return (-1);
    1.40 @@ -308,6 +313,7 @@
    1.41      result = IDirectInput_Initialize(dinput, instance, DIRECTINPUT_VERSION);
    1.42  
    1.43      if (FAILED(result)) {
    1.44 +        SDL_SYS_JoystickQuit();
    1.45          SetDIerror("IDirectInput::Initialize", result);
    1.46          return (-1);
    1.47      }
    1.48 @@ -802,8 +808,15 @@
    1.49          }
    1.50      }
    1.51  
    1.52 -    IDirectInput_Release(dinput);
    1.53 -    dinput = NULL;
    1.54 +    if (dinput != NULL) {
    1.55 +        IDirectInput_Release(dinput);
    1.56 +        dinput = NULL;
    1.57 +    }
    1.58 +
    1.59 +    if (coinitialized) {
    1.60 +        WIN_CoUninitialize();
    1.61 +        coinitialized = SDL_FALSE;
    1.62 +    }
    1.63  }
    1.64  
    1.65  #endif /* SDL_JOYSTICK_DINPUT */