Skip to content

Commit

Permalink
Use RAII for COM initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Jan 28, 2023
1 parent b567078 commit 576e440
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmake/filelists/EngineWin32.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/C4Com.h
src/C4CrashHandlerWin32.cpp
src/C4FileClasses.cpp
src/C4Windows.h
Expand Down
55 changes: 55 additions & 0 deletions src/C4Com.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* LegacyClonk
*
* Copyright (c) 2023, The LegacyClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
*/

#pragma once

#include "C4WinRT.h"

class C4Com
{
public:
C4Com() = default;

explicit C4Com(const winrt::apartment_type apartmentType)
{
MapHResultError(&winrt::init_apartment, apartmentType);
initialized = true;
}

~C4Com()
{
if (initialized)
{
winrt::uninit_apartment();
}
}

C4Com(const C4Com &) = delete;
C4Com &operator=(const C4Com &) = delete;

C4Com(C4Com &&other) noexcept
: initialized{std::exchange(other.initialized, false)}
{
}

C4Com &operator=(C4Com &&other) noexcept
{
initialized = std::exchange(other.initialized, false);
return *this;
}

private:
bool initialized{false};
};
7 changes: 4 additions & 3 deletions src/C4WinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#endif

#ifdef WIN32
#include "C4Com.h"
#include "C4WinRT.h"
#include <objbase.h>
#endif
Expand Down Expand Up @@ -109,18 +110,18 @@ int WINAPI WinMain(HINSTANCE hInst,

SetCurrentProcessExplicitAppUserModelID(_CRT_WIDE(STD_APPUSERMODELID));

C4Com com;

try
{
winrt::init_apartment();
com = C4Com{winrt::apartment_type::multi_threaded};
}
catch (const winrt::hresult_error &e)
{
MessageBoxW(nullptr, (std::wstring{L"Failed to initialize COM: "} + e.message()).c_str(), _CRT_WIDE(STD_PRODUCT), MB_ICONERROR);
return C4XRV_Failure;
}

struct ComUninit { ~ComUninit() { winrt::uninit_apartment(); } } uninit;

// Init application
try
{
Expand Down
8 changes: 2 additions & 6 deletions src/StdWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifdef _WIN32
const int SEC1_TIMER = 1, SEC1_MSEC = 1000;

#include "C4Com.h"
#include "C4WinRT.h"

#include <shobjidl.h>
Expand Down Expand Up @@ -289,16 +290,11 @@ class CStdWindow
virtual bool Win32DialogMessageHandling(MSG *msg) { return false; };

private:
C4Com com{winrt::apartment_type::multi_threaded};
DWORD style = WS_OVERLAPPEDWINDOW;
DWORD styleEx = 0;
winrt::com_ptr<ITaskbarList3> taskBarList{nullptr};

struct ComUnInit
{
ComUnInit() { winrt::init_apartment(); }
~ComUnInit() { winrt::uninit_apartment(); }
} const comUninit;

#elif defined(USE_X11)

protected:
Expand Down

0 comments on commit 576e440

Please sign in to comment.