base/include/base/windows/co_helper.hpp¶
Provides helper utilities for Windows COM operations. More...
Namespaces¶
| Name |
|---|
| cf |
Classes¶
| Name | |
|---|---|
| class | cf::COMHelper Helper class for Windows COM operations. |
Detailed Description¶
Provides helper utilities for Windows COM operations.
Author: Charliechen114514
Version: 0.1
Since: 0.1
Date: 2026-02-22
Contains RAII wrappers and helper functions for managing COM initialization and cleanup.
Source code¶
#pragma once
#include "../expected/expected.hpp"
#include "../scope_guard/scope_guard.hpp"
#include "common.h"
#include <functional>
#include <objbase.h> // CoInitializeEx, CoUninitialize
#include <winerror.h> // HRESULT, FAILED
namespace cf {
template <typename ResourceBack, typename ErrorCode> class COMHelper {
public:
using ContextFunction = std::function<cf::expected<ResourceBack, ErrorCode>()>;
static cf::expected<ResourceBack, ErrorCode>
RunComInterfacesOnce(ContextFunction f, DWORD coinitFlag = COINIT_APARTMENTTHREADED) {
HRESULT hr = ::CoInitializeEx(nullptr, coinitFlag);
if (FAILED(hr)) {
return cf::unexpected<ErrorCode>(static_cast<ErrorCode>(hr));
}
cf::ScopeGuard guard([]() { ::CoUninitialize(); });
hr = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE, nullptr);
if (FAILED(hr)) {
return cf::unexpected<ErrorCode>(static_cast<ErrorCode>(hr));
}
cf::expected<ResourceBack, ErrorCode> result = f();
return result;
}
static cf::expected<ResourceBack, ErrorCode> RunComInterfacesMTA(ContextFunction f) {
return RunComInterfacesOnce(std::move(f), COINIT_MULTITHREADED);
}
};
} // namespace cf
Updated on 2026-03-09 at 10:14:01 +0000