Commit 8982d9f4035e5b74083c2db259e4a87f89c95ed6

Cameron Gutman 2022-04-18T21:19:25

windows: Fix RoInitialize() failure after a CoInitializeEx() call using apartment threading This mirrors the same codepath in WIN_CoInitialize() which handles STA and MTA.

diff --git a/src/core/windows/SDL_windows.c b/src/core/windows/SDL_windows.c
index 5cf0ad9..50e128f 100644
--- a/src/core/windows/SDL_windows.c
+++ b/src/core/windows/SDL_windows.c
@@ -148,7 +148,19 @@ WIN_RoInitialize(void)
     typedef HRESULT (WINAPI *RoInitialize_t)(RO_INIT_TYPE initType);
     RoInitialize_t RoInitializeFunc = (RoInitialize_t)WIN_LoadComBaseFunction("RoInitialize");
     if (RoInitializeFunc) {
-        return RoInitializeFunc(RO_INIT_MULTITHREADED);
+        /* RO_INIT_SINGLETHREADED is equivalent to COINIT_APARTMENTTHREADED */
+        HRESULT hr = RoInitializeFunc(RO_INIT_SINGLETHREADED);
+        if (hr == RPC_E_CHANGED_MODE) {
+            hr = RoInitializeFunc(RO_INIT_MULTITHREADED);
+        }
+
+        /* S_FALSE means success, but someone else already initialized. */
+        /* You still need to call RoUninitialize in this case! */
+        if (hr == S_FALSE) {
+            return S_OK;
+        }
+
+        return hr;
     } else {
         return E_NOINTERFACE;
     }