Skip to content

Commit

Permalink
[win] New base::CoInit class
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Nov 21, 2024
1 parent 7af127f commit e205538
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions base/win/coinit.h
Original file line number Diff line number Diff line change
@@ -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 <objbase.h>

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

0 comments on commit e205538

Please sign in to comment.