Repository navigation
Expand file tree
/
Copy pathModule.cpp
More file actions
192 lines (161 loc) · 5.62 KB
/
Copy pathModule.cpp
File metadata and controls
192 lines (161 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2026 Varjo Technologies Oy. Licensed under the Microsoft Reciprocal License (MS-RL).
// LICENSE.txt has the license text. BAFunctionsCreate and BAFunctionsDestroy follow the BAFunctions
// sample of the WiX Toolset, copyright (c) .NET Foundation and contributors.
// The entry points of the DLL. WixStdBA calls BAFunctionsCreate after it has loaded the DLL, and
// BAFunctionsDestroy on the thread of the engine events before it unloads the DLL. By then the UI
// thread has ended and the main window is gone.
#include "ChoiceDialog.hpp"
#include "Dimmer.hpp"
#include "Images.hpp"
#include "VarjoBAFunctions.hpp"
#include "WindowUtil.hpp"
#include <windows.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4458) // GDI+ headers hide class members.
#endif
#include <gdiplus.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <BalBaseBAFunctionsProc.h>
#include <balutil.h>
#include <memory>
#include <new>
namespace Varjo
{
namespace
{
// The logging and the engine functions of balutil, which BalInitialize starts.
class BalSession
{
public:
explicit BalSession(IBootstrapperEngine* engine);
~BalSession();
BalSession(const BalSession&) = delete;
BalSession& operator=(const BalSession&) = delete;
};
class GdiplusSession
{
public:
GdiplusSession();
~GdiplusSession();
GdiplusSession(const GdiplusSession&) = delete;
GdiplusSession& operator=(const GdiplusSession&) = delete;
bool isRunning() const;
private:
ULONG_PTR m_token = 0;
bool m_running = false;
};
struct BAFunctionsReleaser {
void operator()(VarjoBAFunctions* functions) const { functions->Release(); }
};
// Everything that the DLL holds between BAFunctionsCreate and BAFunctionsDestroy. The members go in
// the reverse order: the BAFunctions object before the images, and the images before GDI+.
class Module
{
public:
Module(HINSTANCE instance, IBootstrapperEngine* engine);
Module(const Module&) = delete;
Module& operator=(const Module&) = delete;
HRESULT createBAFunctions(const BA_FUNCTIONS_CREATE_ARGS& args, BA_FUNCTIONS_CREATE_RESULTS& results);
private:
const HINSTANCE m_instance;
BalSession m_bal;
GdiplusSession m_gdiplus;
WindowClass m_dimmerClass;
WindowClass m_choiceDialogClass;
ImageCache m_images;
std::unique_ptr<VarjoBAFunctions, BAFunctionsReleaser> m_functions;
};
HRESULT WINAPI varjoBAFunctionsProc(BA_FUNCTIONS_MESSAGE message, const LPVOID args, LPVOID results, LPVOID context);
HINSTANCE g_instance = nullptr;
// The module lives from BAFunctionsCreate to BAFunctionsDestroy. The pointer is raw, because a
// global with a destructor would run it in DLL_PROCESS_DETACH, under the loader lock.
Module* g_module = nullptr;
} // namespace
} // namespace Varjo
extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID)
{
if (reason == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(instance);
Varjo::g_instance = instance;
}
return TRUE;
}
extern "C" HRESULT WINAPI BAFunctionsCreate(const BA_FUNCTIONS_CREATE_ARGS* args, BA_FUNCTIONS_CREATE_RESULTS* results)
{
// The module allocates while it starts. WixStdBA is C++ code that does not expect exceptions, so
// a failed allocation becomes an error code here.
try {
auto module = std::make_unique<Varjo::Module>(Varjo::g_instance, args->pEngine);
const HRESULT hr = module->createBAFunctions(*args, *results);
if (SUCCEEDED(hr)) {
Varjo::g_module = module.release();
}
return hr;
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
}
}
extern "C" void WINAPI BAFunctionsDestroy(const BA_FUNCTIONS_DESTROY_ARGS*, BA_FUNCTIONS_DESTROY_RESULTS*)
{
const std::unique_ptr<Varjo::Module> module(Varjo::g_module);
Varjo::g_module = nullptr;
}
namespace Varjo
{
namespace
{
BalSession::BalSession(IBootstrapperEngine* engine) { BalInitialize(engine); }
BalSession::~BalSession() { BalUninitialize(); }
GdiplusSession::GdiplusSession()
{
const Gdiplus::GdiplusStartupInput input;
m_running = Gdiplus::GdiplusStartup(&m_token, &input, nullptr) == Gdiplus::Ok;
}
GdiplusSession::~GdiplusSession()
{
if (m_running) {
Gdiplus::GdiplusShutdown(m_token);
}
}
bool GdiplusSession::isRunning() const { return m_running; }
Module::Module(HINSTANCE instance, IBootstrapperEngine* engine)
: m_instance(instance)
, m_bal(engine)
, m_dimmerClass(Dimmer::windowClass(instance))
, m_choiceDialogClass(choiceDialogWindowClass(instance))
, m_images(instance)
{
}
HRESULT Module::createBAFunctions(const BA_FUNCTIONS_CREATE_ARGS& args, BA_FUNCTIONS_CREATE_RESULTS& results)
{
if (!m_gdiplus.isRunning()) {
return E_FAIL;
}
m_functions.reset(new (std::nothrow) VarjoBAFunctions(m_instance, m_images));
if (!m_functions) {
return E_OUTOFMEMORY;
}
const HRESULT hr = m_functions->OnCreate(args.pEngine, args.pCommand);
if (FAILED(hr)) {
m_functions.reset();
return hr;
}
results.pfnBAFunctionsProc = varjoBAFunctionsProc;
results.pvBAFunctionsProcContext = m_functions.get();
return S_OK;
}
// WixStdBA passes every event to the DLL through this function. The events of the engine have the
// numbers below the first message of the theme.
HRESULT WINAPI varjoBAFunctionsProc(BA_FUNCTIONS_MESSAGE message, const LPVOID args, LPVOID results, LPVOID context)
{
const HRESULT hr = BalBaseBAFunctionsProc(message, args, results, context);
if (message < BA_FUNCTIONS_MESSAGE_ONTHEMELOADED) {
static_cast<VarjoBAFunctions*>(context)->finishEngineEvent();
}
return hr;
}
} // namespace
} // namespace Varjo