From a19ed46b2a55dc93e5792c84e57a7c45b89f97a0 Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Fri, 14 Oct 2022 12:49:41 +0200 Subject: [PATCH] Windows: Fix file and folder dialog freeze (#369) Initializing the COM library immediately seems to be more robust than doing it on demand --- src/audio/CubebAPI.cpp | 18 ------------------ src/audio/CubebAPI.h | 1 - src/audio/XAudio27API.cpp | 9 --------- src/audio/XAudio27API.h | 1 - src/audio/XAudio2API.cpp | 14 -------------- src/audio/XAudio2API.h | 1 - src/main.cpp | 10 +++++++--- 7 files changed, 7 insertions(+), 47 deletions(-) diff --git a/src/audio/CubebAPI.cpp b/src/audio/CubebAPI.cpp index c40e0f55..8b2a235f 100644 --- a/src/audio/CubebAPI.cpp +++ b/src/audio/CubebAPI.cpp @@ -167,25 +167,11 @@ void CubebAPI::SetVolume(sint32 volume) bool CubebAPI::InitializeStatic() { -#if BOOST_OS_WINDOWS - s_com_initialized = (SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED))); -#endif - if (cubeb_init(&s_context, "Cemu Cubeb", nullptr)) { cemuLog_force("can't create cubeb audio api"); - -#if BOOST_OS_WINDOWS - if (s_com_initialized) - { - CoUninitialize(); - s_com_initialized = false; - } -#endif - return false; } - return true; } @@ -193,10 +179,6 @@ void CubebAPI::Destroy() { if (s_context) cubeb_destroy(s_context); -#if BOOST_OS_WINDOWS - if (s_com_initialized) - CoUninitialize(); -#endif } std::vector CubebAPI::GetDevices() diff --git a/src/audio/CubebAPI.h b/src/audio/CubebAPI.h index a828ce0d..2dce9374 100644 --- a/src/audio/CubebAPI.h +++ b/src/audio/CubebAPI.h @@ -41,7 +41,6 @@ public: static void Destroy(); private: - inline static bool s_com_initialized = false; inline static cubeb* s_context = nullptr; cubeb_stream* m_stream = nullptr; diff --git a/src/audio/XAudio27API.cpp b/src/audio/XAudio27API.cpp index 4a99bfa2..fadd02f8 100644 --- a/src/audio/XAudio27API.cpp +++ b/src/audio/XAudio27API.cpp @@ -5,7 +5,6 @@ static_assert(IAudioAPI::kBlockCount < XAUDIO2_MAX_QUEUED_BUFFERS, "too many xaudio2 buffers"); HMODULE XAudio27API::s_xaudio_dll = nullptr; -bool XAudio27API::s_com_initialized = false; std::unique_ptr XAudio27API::s_xaudio; XAudio27API::XAudio27API(uint32 device_id, uint32 samplerate, uint32 channels, uint32 samples_per_block, uint32 bits_per_sample) @@ -115,8 +114,6 @@ bool XAudio27API::InitializeStatic() if (s_xaudio) return true; - s_com_initialized = (SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE))); - #ifdef _DEBUG s_xaudio_dll = LoadLibraryExW(L"XAudioD2_7.DLL", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); if(!s_xaudio_dll) @@ -142,9 +139,6 @@ bool XAudio27API::InitializeStatic() if (s_xaudio_dll) FreeLibrary(s_xaudio_dll); - if (s_com_initialized) - CoUninitialize(); - return false; } } @@ -155,9 +149,6 @@ void XAudio27API::Destroy() if (s_xaudio_dll) FreeLibrary(s_xaudio_dll); - - if (s_com_initialized) - CoUninitialize(); } std::vector XAudio27API::GetDevices() diff --git a/src/audio/XAudio27API.h b/src/audio/XAudio27API.h index 80e423a3..badab8f6 100644 --- a/src/audio/XAudio27API.h +++ b/src/audio/XAudio27API.h @@ -58,7 +58,6 @@ private: }; static HMODULE s_xaudio_dll; - static bool s_com_initialized; static std::unique_ptr s_xaudio; std::unique_ptr m_xaudio; diff --git a/src/audio/XAudio2API.cpp b/src/audio/XAudio2API.cpp index fd0a305b..9759210d 100644 --- a/src/audio/XAudio2API.cpp +++ b/src/audio/XAudio2API.cpp @@ -23,7 +23,6 @@ static const GUID DEVINTERFACE_AUDIO_RENDER_GUID = { 0xe6327cad, 0xdcec, 0x4949, static_assert(IAudioAPI::kBlockCount < XAUDIO2_MAX_QUEUED_BUFFERS, "too many xaudio2 buffers"); HMODULE XAudio2API::s_xaudio_dll = nullptr; -bool XAudio2API::s_com_initialized = false; std::vector XAudio2API::s_devices; XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channels, uint32 samples_per_block, uint32 bits_per_sample) @@ -143,8 +142,6 @@ bool XAudio2API::InitializeStatic() { if (s_xaudio_dll) return true; - - s_com_initialized = (SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED))); // win 10 s_xaudio_dll = LoadLibraryEx(XAUDIO2_DLL, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); @@ -166,9 +163,6 @@ bool XAudio2API::InitializeStatic() if (s_xaudio_dll) FreeLibrary(s_xaudio_dll); - if (s_com_initialized) - CoUninitialize(); - return false; } } @@ -177,20 +171,12 @@ void XAudio2API::Destroy() { if (s_xaudio_dll) FreeLibrary(s_xaudio_dll); - - if (s_com_initialized) - CoUninitialize(); } const std::vector& XAudio2API::RefreshDevices() { - // this function must be called from the same thread as we called CoInitializeEx s_devices.clear(); - HRESULT r = CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); - if (r != RPC_E_CHANGED_MODE && FAILED(r)) - return s_devices; - try { struct IWbemLocator *wbem_locator = nullptr; diff --git a/src/audio/XAudio2API.h b/src/audio/XAudio2API.h index 2b23e1cc..1f7057f0 100644 --- a/src/audio/XAudio2API.h +++ b/src/audio/XAudio2API.h @@ -59,7 +59,6 @@ private: }; static HMODULE s_xaudio_dll; - static bool s_com_initialized; static std::vector s_devices; std::unique_ptr m_xaudio; diff --git a/src/main.cpp b/src/main.cpp index e1c8ca49..323494c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -323,10 +323,10 @@ void HandlePostUpdate() fs::remove(filename, ec); } #else - while( fs::exists(filename) ) + while (fs::exists(filename)) { std::error_code ec; - fs::remove(filename, ec); + fs::remove(filename, ec); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } #endif @@ -338,8 +338,10 @@ void ToolShaderCacheMerger(); #if BOOST_OS_WINDOWS // entrypoint for release builds -int wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nShowCmd) +int wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nShowCmd) { + if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE))) + cemuLog_log(LogType::Force, "CoInitializeEx() failed"); SDL_SetMainReady(); if (!LaunchSettings::HandleCommandline(lpCmdLine)) return 0; @@ -350,6 +352,8 @@ int wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ L // entrypoint for debug builds with console int main(int argc, char* argv[]) { + if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE))) + cemuLog_log(LogType::Force, "CoInitializeEx() failed"); SDL_SetMainReady(); if (!LaunchSettings::HandleCommandline(argc, argv)) return 0;