From e2055386fb2c786594afbc1c978471498e9cdf8f Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 14 Nov 2024 09:02:12 -0300 Subject: [PATCH] [win] New base::CoInit class --- base/win/coinit.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 base/win/coinit.h diff --git a/base/win/coinit.h b/base/win/coinit.h new file mode 100644 index 000000000..3a446ddbd --- /dev/null +++ b/base/win/coinit.h @@ -0,0 +1,35 @@ +// LAF Base Library +// Copyright (c) 2024 Igara Studio S.A. +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef BASE_WIN_COINIT_H_INCLUDED +#define BASE_WIN_COINIT_H_INCLUDED +#pragma once + +#if !LAF_WINDOWS + #error This header file can be used only on Windows platform +#endif + +#include + +namespace base { + +// Successful calls to CoInitialize() (S_OK or S_FALSE) must match +// the calls to CoUninitialize(). +// From: +// https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize#remarks +struct CoInit { + HRESULT hr; + CoInit() { hr = CoInitialize(nullptr); } + ~CoInit() + { + if (hr == S_OK || hr == S_FALSE) + CoUninitialize(); + } +}; + +} // namespace base + +#endif