From 8982d9f4035e5b74083c2db259e4a87f89c95ed6 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 18 Apr 2022 21:19:25 -0500 Subject: [PATCH] windows: Fix RoInitialize() failure after a CoInitializeEx() call using apartment threading This mirrors the same codepath in WIN_CoInitialize() which handles STA and MTA. --- src/core/windows/SDL_windows.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/windows/SDL_windows.c b/src/core/windows/SDL_windows.c index 5cf0ad90d..50e128fcc 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; }