From 60fbe1fd0b821e044bf47628a75cffa3bc757234 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sun, 10 May 2026 16:37:00 +0800 Subject: [PATCH] Common (Windows): uses WinRT APIs only when `FF_HAVE_WINRT` is defined --- CMakeLists.txt | 7 +++---- src/common/windows/com.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eca886844..c2c84cd5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1340,9 +1340,9 @@ if(WIN32) include(CheckIncludeFileCXX) CHECK_INCLUDE_FILE_CXX("winrt/Windows.Foundation.h" HAVE_WINRT) if(HAVE_WINRT) - message(STATUS "WinRT headers are available, media detection will use WinRT APIs") + message(STATUS "WinRT headers are available, media detection will be enabled") else() - message(STATUS "WinRT headers are NOT available, media detection will be disabled") + message(WARNING "WinRT headers are NOT available, media detection will be disabled") endif() endif() include(CheckFunctionExists) @@ -1759,8 +1759,7 @@ elseif(WIN32) endif() if(HAVE_WINRT) target_link_libraries(libfastfetch - PRIVATE - "RuntimeObject" + PRIVATE "runtimeobject" ) endif() elseif(FreeBSD) diff --git a/src/common/windows/com.c b/src/common/windows/com.c index 0eb566f8b..3f2fa3987 100644 --- a/src/common/windows/com.c +++ b/src/common/windows/com.c @@ -1,6 +1,7 @@ #include "com.h" -#include +#if FF_HAVE_WINRT + #include static void RoUninitializeWrap(void) { RoUninitialize(); @@ -27,6 +28,33 @@ static const char* doInitCom() { atexit(RoUninitializeWrap); return NULL; } +#else + #include + +static void CoUninitializeWrap(void) { + CoUninitialize(); +} + +static const char* doInitCom() { + HRESULT res = CoInitializeEx(NULL, COINIT_MULTITHREADED); + if (FAILED(res)) { + switch (res) { + case E_INVALIDARG: + return "CoInitializeEx() failed: invalid argument"; + case E_OUTOFMEMORY: + return "CoInitializeEx() failed: out of memory"; + case RPC_E_CHANGED_MODE: + // COM was already initialized with a different concurrency model + return NULL; + default: + return "CoInitializeEx() failed: unknown error"; + } + } + + atexit(CoUninitializeWrap); + return NULL; +} +#endif const char* ffInitCom(void) { static const char* error = "";