From e80d210dcc7e2b45656dfe29cb42e4363a088b05 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 9 Feb 2019 10:32:32 +0100 Subject: proto/simconnect: move activation context to compat --- compat/activation-context.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ compat/activation-context.hpp | 26 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 compat/activation-context.cpp create mode 100644 compat/activation-context.hpp (limited to 'compat') diff --git a/compat/activation-context.cpp b/compat/activation-context.cpp new file mode 100644 index 00000000..8d34243d --- /dev/null +++ b/compat/activation-context.cpp @@ -0,0 +1,51 @@ +#ifdef _WIN32 + +#include "activation-context.hpp" +#include "compat/library-path.hpp" + +#include +#include +#include + +#include + +static_assert(sizeof(std::uintptr_t) == sizeof(ULONG_PTR)); + +activation_context::activation_context(const QString& module_name, int resid) +{ + static const QString prefix = OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH + OPENTRACK_LIBRARY_PREFIX; + QString path = prefix + module_name; + QByteArray name = QFile::encodeName(path); + + ACTCTXA actx = {}; + actx.cbSize = sizeof(actx); + actx.lpResourceName = MAKEINTRESOURCEA(resid); + actx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID; + actx.lpSource = name.constData(); + + handle = CreateActCtxA(&actx); + + if (handle != INVALID_HANDLE_VALUE) + { + if (!ActivateActCtx(handle, (ULONG_PTR*)&cookie)) + { + qDebug() << "win32: can't set activation context" << GetLastError(); + ReleaseActCtx(handle); + handle = INVALID_HANDLE_VALUE; + } + else + ok = true; + } else { + qDebug() << "win32: can't create activation context" << GetLastError(); + } +} + +activation_context::~activation_context() +{ + if (handle != INVALID_HANDLE_VALUE) + { + DeactivateActCtx(0, cookie); + ReleaseActCtx(handle); + } +} +#endif diff --git a/compat/activation-context.hpp b/compat/activation-context.hpp new file mode 100644 index 00000000..a3b0429e --- /dev/null +++ b/compat/activation-context.hpp @@ -0,0 +1,26 @@ +#pragma once + +#ifdef _WIN32 + +#include "export.hpp" + +#include +#include + +class OTR_COMPAT_EXPORT activation_context +{ +public: + explicit activation_context(const QString& module_name, int resid); + ~activation_context(); + + explicit operator bool() const { return ok; } + +private: + std::uintptr_t cookie = 0; + void* handle = (void*)-1; + bool ok = false; +}; + +#else +# error "tried to use win32-only activation context" +#endif -- cgit v1.2.3