Misc: another COM code cleanup

This commit is contained in:
Oleg Tolmatcev
2026-07-03 16:08:58 -04:00
committed by Ty
parent 806f06fba6
commit 16b6572826
3 changed files with 14 additions and 22 deletions
+3 -3
View File
@@ -376,9 +376,11 @@ bool TAPGetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter,
HRESULT cohr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); HRESULT cohr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (cohr == RPC_E_CHANGED_MODE) if (cohr == RPC_E_CHANGED_MODE)
cohr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); cohr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (!SUCCEEDED(cohr)) if (FAILED(cohr))
return false; return false;
wil::unique_couninitialize_call uninit;
PIP_ADAPTER_ADDRESSES bridgeAdapter = nullptr; PIP_ADAPTER_ADDRESSES bridgeAdapter = nullptr;
//Create Instance of INetCfg //Create Instance of INetCfg
@@ -472,8 +474,6 @@ bool TAPGetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter,
} }
} }
CoUninitialize();
if (bridgeAdapter != nullptr) if (bridgeAdapter != nullptr)
{ {
*adapter = *bridgeAdapter; *adapter = *bridgeAdapter;
+6 -9
View File
@@ -13,6 +13,7 @@
#include "USB/USB.h" #include "USB/USB.h"
#include <jpeglib.h> #include <jpeglib.h>
#include <wil/com.h>
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic push #pragma clang diagnostic push
@@ -57,16 +58,15 @@ namespace usb_eyetoy
{ {
std::vector<std::pair<std::string, std::string>> devList; std::vector<std::pair<std::string, std::string>> devList;
ICreateDevEnum* pCreateDevEnum = nullptr; wil::com_ptr_nothrow<ICreateDevEnum> pCreateDevEnum = wil::CoCreateInstanceNoThrow<ICreateDevEnum>(CLSID_SystemDeviceEnum);
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pCreateDevEnum)); if (!pCreateDevEnum)
if (FAILED(hr))
{ {
Console.Warning("Camera: Error Creating Device Enumerator"); Console.Warning("Camera: Error Creating Device Enumerator");
return devList; return devList;
} }
wil::com_ptr_nothrow<IEnumMoniker> pEnum; wil::com_ptr_nothrow<IEnumMoniker> pEnum;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0); HRESULT hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
if (hr != S_OK) if (hr != S_OK)
{ {
Console.Warning("Camera: You have no video capture hardware"); Console.Warning("Camera: You have no video capture hardware");
@@ -97,8 +97,6 @@ namespace usb_eyetoy
} }
} }
CoUninitialize();
return devList; return devList;
} }
@@ -136,9 +134,8 @@ namespace usb_eyetoy
} }
// enumerate all video capture devices // enumerate all video capture devices
ICreateDevEnum* pCreateDevEnum = nullptr; wil::com_ptr_nothrow<ICreateDevEnum> pCreateDevEnum = wil::CoCreateInstanceNoThrow<ICreateDevEnum>(CLSID_SystemDeviceEnum);
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pCreateDevEnum)); if (!pCreateDevEnum)
if (FAILED(hr))
{ {
Console.Warning("Camera: Error Creating Device Enumerator"); Console.Warning("Camera: Error Creating Device Enumerator");
return -1; return -1;
+5 -10
View File
@@ -6,7 +6,6 @@
#include "common/FileSystem.h" #include "common/FileSystem.h"
#include "common/Console.h" #include "common/Console.h"
#include "common/ScopedGuard.h"
#include "common/StringUtil.h" #include "common/StringUtil.h"
#include "common/ProgressCallback.h" #include "common/ProgressCallback.h"
#include "common/RedtapeWilCom.h" #include "common/RedtapeWilCom.h"
@@ -18,6 +17,7 @@
#include <thread> #include <thread>
#include <wil/resource.h> #include <wil/resource.h>
#include <wil/result.h>
#include <wil/win32_helpers.h> #include <wil/win32_helpers.h>
#pragma comment(lib, "synchronization.lib") #pragma comment(lib, "synchronization.lib")
@@ -76,7 +76,7 @@ private:
wil::com_ptr_nothrow<ITaskbarList3> m_taskbar_list; wil::com_ptr_nothrow<ITaskbarList3> m_taskbar_list;
int m_last_progress_percent = -1; int m_last_progress_percent = -1;
bool m_com_initialized = false; wil::unique_couninitialize_call m_com_initialized{false};
static inline const UINT s_uTBBC = RegisterWindowMessageW(L"TaskbarButtonCreated"); static inline const UINT s_uTBBC = RegisterWindowMessageW(L"TaskbarButtonCreated");
static constexpr UINT WMAPP_SETTASKBARPROGRESS = WM_USER+0; static constexpr UINT WMAPP_SETTASKBARPROGRESS = WM_USER+0;
@@ -192,7 +192,7 @@ bool Win32ProgressCallback::Create()
class_registered = true; class_registered = true;
} }
m_com_initialized = SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)); m_com_initialized = wil::CoInitializeEx_failfast(COINIT_APARTMENTTHREADED);
m_window_hwnd = m_window_hwnd =
CreateWindowExW(WS_EX_CLIENTEDGE, CLASS_NAME, L"Win32ProgressCallback", WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, CW_USEDEFAULT, CreateWindowExW(WS_EX_CLIENTEDGE, CLASS_NAME, L"Win32ProgressCallback", WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, CW_USEDEFAULT,
@@ -222,8 +222,7 @@ void Win32ProgressCallback::Destroy()
m_progress_hwnd = {}; m_progress_hwnd = {};
} }
if (m_com_initialized) m_com_initialized.reset();
CoUninitialize();
} }
void Win32ProgressCallback::PumpMessages() void Win32ProgressCallback::PumpMessages()
@@ -420,11 +419,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
{ {
Win32ProgressCallback progress; Win32ProgressCallback progress;
const bool com_initialized = SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)); wil::unique_couninitialize_call uninit = wil::CoInitializeEx_failfast(COINIT_MULTITHREADED);
const ScopedGuard com_guard = [com_initialized]() {
if (com_initialized)
CoUninitialize();
};
int argc = 0; int argc = 0;
wil::unique_hlocal_ptr<LPWSTR[]> argv(CommandLineToArgvW(GetCommandLineW(), &argc)); wil::unique_hlocal_ptr<LPWSTR[]> argv(CommandLineToArgvW(GetCommandLineW(), &argc));