Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit 397aa55

Browse files
committed
reduce dynamic_initializers
1 parent 84d2468 commit 397aa55

15 files changed

+179
-185
lines changed

Common/Api.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ bool Api::InteractiveIO::OutputToConsole(
153153
}
154154

155155
auto outputString{std::format(L"{}{}{}", prefixStr, std::wstring_view{ buffer, static_cast<size_t>(length) }, additionalStr)};
156-
std::wcout << outputString;
156+
wprintf_s(outputString.c_str());
157157
#ifdef _DEBUG
158158
OutputDebugStringW(outputString.c_str());
159159
#endif
@@ -199,15 +199,10 @@ void Api::InteractiveIO::Startup()
199199
{
200200
if (GetConsoleWindow() || AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
201201
{
202-
FILE* fpstdin{ stdin }, * fpstdout{ stdout }, * fpstderr{ stderr };
202+
FILE* fpstdin{ stdin }, * fpstdout{ stdout };
203203
_wfreopen_s(&fpstdin, L"CONIN$", L"r", stdin);
204-
_wfreopen_s(&fpstdout, L"CONOUT$", L"w", stdout);
205-
_wfreopen_s(&fpstderr, L"CONOUT$", L"w", stderr);
206-
207-
std::ios_base::sync_with_stdio(false);
208-
std::wcin.tie(0);
209-
210-
std::wcout.imbue(std::locale("chs"));
204+
_wfreopen_s(&fpstdout, L"CONOUT$", L"w+t", stdout);
205+
_wsetlocale(LC_ALL, L"");
211206
}
212207
}
213208

Common/HookHelper.hpp

+53-11
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,39 @@ namespace TranslucentFlyouts
5858
constexpr UINT LDR_DLL_NOTIFICATION_REASON_LOADED{ 1 };
5959
constexpr UINT LDR_DLL_NOTIFICATION_REASON_UNLOADED{ 2 };
6060

61-
inline const auto g_actualLdrRegisterDllNotification{ reinterpret_cast<NTSTATUS(NTAPI*)(ULONG, HookHelper::PLDR_DLL_NOTIFICATION_FUNCTION, PVOID, PVOID*)>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrRegisterDllNotification")) };
62-
inline const auto g_actualLdrUnregisterDllNotification{ reinterpret_cast<NTSTATUS(NTAPI*)(PVOID)>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrUnregisterDllNotification")) };
61+
__forceinline NTSTATUS NTAPI LdrRegisterDllNotification(
62+
ULONG flags,
63+
PLDR_DLL_NOTIFICATION_FUNCTION notificationFunction,
64+
PVOID context,
65+
PVOID* cookie
66+
)
67+
{
68+
static const auto s_actualLdrRegisterDllNotification{ reinterpret_cast<NTSTATUS(NTAPI*)(ULONG, HookHelper::PLDR_DLL_NOTIFICATION_FUNCTION, PVOID, PVOID*)>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrRegisterDllNotification")) };
69+
if (s_actualLdrRegisterDllNotification) [[likely]]
70+
{
71+
return s_actualLdrRegisterDllNotification(
72+
flags,
73+
notificationFunction,
74+
context,
75+
cookie
76+
);
77+
}
78+
79+
return 0xC000000F;
80+
}
81+
__forceinline NTSTATUS NTAPI LdrUnregisterDllNotification(PVOID cookie)
82+
{
83+
static const auto s_actualLdrUnregisterDllNotification{ reinterpret_cast<NTSTATUS(NTAPI*)(PVOID)>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "LdrUnregisterDllNotification")) };
84+
if (s_actualLdrUnregisterDllNotification) [[likely]]
85+
{
86+
return s_actualLdrUnregisterDllNotification(
87+
cookie
88+
);
89+
}
6390

91+
return 0xC000000F;
92+
}
93+
6494
void WriteMemory(PVOID memoryAddress, const std::function<void()>&& callback);
6595
PVOID InjectCallbackToThread(DWORD threadId, const std::function<void()>& callback);
6696
HMODULE GetProcessModule(HANDLE processHandle, std::wstring_view dllPath);
@@ -134,9 +164,21 @@ namespace TranslucentFlyouts
134164
void Detach(PVOID* realFuncAddr, PVOID hookFuncAddr) noexcept(false);
135165
}
136166

137-
inline UINT TFM_REMOVESUBCLASS{ RegisterWindowMessageW(L"TranslucentFlyouts.RemoveWindowSublcass") };
138-
inline UINT TFM_ATTACH{ RegisterWindowMessageW(L"TranslucentFlyouts.Attach") };
139-
inline UINT TFM_DETACH{ RegisterWindowMessageW(L"TranslucentFlyouts.Detach") };
167+
__forceinline UINT GetRemoveSubclassMsg()
168+
{
169+
static UINT TFM_REMOVESUBCLASS{ RegisterWindowMessageW(L"TranslucentFlyouts.RemoveWindowSublcass") };
170+
return TFM_REMOVESUBCLASS;
171+
}
172+
__forceinline UINT GetAttachMsg()
173+
{
174+
static UINT TFM_ATTACH{ RegisterWindowMessageW(L"TranslucentFlyouts.Attach") };
175+
return TFM_ATTACH;
176+
}
177+
__forceinline UINT GetDetachMsg()
178+
{
179+
static UINT TFM_DETACH{ RegisterWindowMessageW(L"TranslucentFlyouts.Detach") };
180+
return TFM_DETACH;
181+
}
140182
inline constexpr std::wstring_view subclassPropPrefix{ L"TranslucentFlyouts.Token" };
141183

142184
namespace HwndProp
@@ -198,7 +240,7 @@ namespace TranslucentFlyouts
198240
if (SetWindowSubclass(hwnd, Storage<subclassProc>::Wrapper, 0, 0))
199241
{
200242
windowList.push_back(hwnd);
201-
Storage<subclassProc>::Wrapper(hwnd, TFM_ATTACH, 0, 0, 0, 0);
243+
Storage<subclassProc>::Wrapper(hwnd, GetAttachMsg(), 0, 0, 0, 0);
202244
}
203245
};
204246

@@ -217,7 +259,7 @@ namespace TranslucentFlyouts
217259
{
218260
if (threadId == GetCurrentThreadId())
219261
{
220-
Storage<subclassProc>::Wrapper(hwnd, TFM_DETACH, windowDestroyed, 0, 0, 0);
262+
Storage<subclassProc>::Wrapper(hwnd, GetDetachMsg(), windowDestroyed, 0, 0, 0);
221263
if (RemoveWindowSubclass(hwnd, Storage<subclassProc>::Wrapper, 0))
222264
{
223265
windowList.erase(std::remove(windowList.begin(), windowList.end(), hwnd), windowList.end());
@@ -226,7 +268,7 @@ namespace TranslucentFlyouts
226268
}
227269
else
228270
{
229-
SendMessageW(hwnd, TFM_REMOVESUBCLASS, 0, 0);
271+
SendMessageW(hwnd, GetRemoveSubclassMsg(), 0, 0);
230272
}
231273
}
232274
}
@@ -250,7 +292,7 @@ namespace TranslucentFlyouts
250292
DWORD_PTR dwRefData
251293
)
252294
{
253-
if (uMsg == TFM_REMOVESUBCLASS)
295+
if (uMsg == GetRemoveSubclassMsg())
254296
{
255297
Attach<subclassProc>(hWnd, false);
256298
return 0;
@@ -294,12 +336,12 @@ namespace TranslucentFlyouts
294336
if (value)
295337
{
296338
windowList.push_back(hwnd);
297-
Storage<subclassProc>::Wrapper(hwnd, TFM_ATTACH, windowDestroyed, 0);
339+
Storage<subclassProc>::Wrapper(hwnd, GetAttachMsg(), windowDestroyed, 0);
298340
}
299341
}
300342
if (!attach && refCount == 0)
301343
{
302-
Storage<subclassProc>::Wrapper(hwnd, TFM_DETACH, windowDestroyed, 0);
344+
Storage<subclassProc>::Wrapper(hwnd, GetDetachMsg(), windowDestroyed, 0);
303345
if (SetWindowLongPtrW(hwnd, GWLP_WNDPROC, HwndProp::Get<LONG_PTR>(hwnd, GetNamespace<subclassProc>(), propName)))
304346
{
305347
windowList.erase(std::remove(windowList.begin(), windowList.end(), hwnd), windowList.end());

TFMain/Application.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ using namespace TranslucentFlyouts;
99

1010
namespace TranslucentFlyouts::Application
1111
{
12-
const UINT TFM_STOP{ RegisterWindowMessageW(L"TranslucentFlyouts.Win32.Stop") };
12+
UINT GetStopMsg()
13+
{
14+
const UINT TFM_STOP{ RegisterWindowMessageW(L"TranslucentFlyouts.Win32.Stop") };
15+
return TFM_STOP;
16+
}
1317
}
1418

1519
HRESULT Application::InstallHook()
@@ -75,11 +79,11 @@ HRESULT Application::StartService()
7579
nullptr, nullptr, wil::GetModuleInstanceHandle(), nullptr
7680
);
7781
RETURN_LAST_ERROR_IF_NULL(serviceInfo->hostWindow);
78-
RETURN_IF_WIN32_BOOL_FALSE(ChangeWindowMessageFilterEx(serviceInfo->hostWindow, TFM_STOP, MSGFLT_ALLOW, nullptr));
82+
RETURN_IF_WIN32_BOOL_FALSE(ChangeWindowMessageFilterEx(serviceInfo->hostWindow, GetStopMsg(), MSGFLT_ALLOW, nullptr));
7983

8084
auto callback = [](HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR, DWORD_PTR) -> LRESULT
8185
{
82-
if (uMsg == WM_ENDSESSION || uMsg == TFM_STOP)
86+
if (uMsg == WM_ENDSESSION || uMsg == GetStopMsg())
8387
{
8488
DestroyWindow(hWnd);
8589
}
@@ -116,7 +120,7 @@ HRESULT Application::StopService()
116120
RETURN_LAST_ERROR_IF_NULL(serviceInfo);
117121
RETURN_HR_IF_NULL(HRESULT_FROM_WIN32(ERROR_SERVICE_START_HANG), serviceInfo->hostWindow);
118122
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), !IsWindow(serviceInfo->hostWindow));
119-
RETURN_LAST_ERROR_IF(!SendNotifyMessageW(serviceInfo->hostWindow, TFM_STOP, 0, 0));
123+
RETURN_LAST_ERROR_IF(!SendNotifyMessageW(serviceInfo->hostWindow, GetStopMsg(), 0, 0));
120124

121125
UINT frameCount{ 0 };
122126
constexpr UINT maxWaitFrameCount{ 1500 };

TFMain/DXHelper.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88
using namespace TranslucentFlyouts;
99
using namespace TranslucentFlyouts::DXHelper;
1010

11-
LazyDX::InternalHook LazyDX::g_internalHook{};
12-
1311
BOOL WINAPI LazyDX::InternalHook::FreeLibrary(
1412
HMODULE hLibModule
1513
)
1614
{
17-
auto actualFreeLibrary{ g_internalHook.actualFreeLibrary };
15+
auto actualFreeLibrary{ GetInternalHook().actualFreeLibrary };
1816

1917
if (hLibModule == wil::GetModuleInstanceHandle())
2018
{
2119
auto f = [](PTP_CALLBACK_INSTANCE pci, PVOID)
2220
{
23-
auto dxList{ g_internalHook.dxList };
21+
auto dxList{ GetInternalHook().dxList };
2422

2523
for (auto it = dxList.begin(); it != dxList.end(); it++)
2624
{
@@ -49,7 +47,7 @@ BOOL WINAPI LazyDX::InternalHook::FreeLibrary(
4947

5048
void LazyDX::NotifyDeviceLost()
5149
{
52-
auto& dxList{g_internalHook.dxList};
50+
auto& dxList{ GetInternalHook().dxList };
5351
for (auto it = dxList.begin(); it != dxList.end(); it++)
5452
{
5553
auto& lazyDX{*it};
@@ -100,15 +98,21 @@ void LazyDX::InternalHook::ShutdownHook()
10098

10199
LazyDX::LazyDX()
102100
{
103-
g_internalHook.dxList.push_back(this);
101+
GetInternalHook().dxList.push_back(this);
104102
}
105103

106104
LazyDX::~LazyDX()
107105
{
108-
auto& dxList{ g_internalHook.dxList };
106+
auto& dxList{ GetInternalHook().dxList};
109107
dxList.erase(std::find(dxList.begin(), dxList.end(), this));
110108
}
111109

110+
LazyDX::InternalHook& LazyDX::GetInternalHook()
111+
{
112+
static InternalHook s_internalHook{};
113+
return s_internalHook;
114+
}
115+
112116
/* ======================================================================================== */
113117

114118
LazyD2D& LazyD2D::GetInstance()

TFMain/DXHelper.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace TranslucentFlyouts
5555
std::vector<LazyDX*> dxList;
5656
decltype(FreeLibrary)* actualFreeLibrary{nullptr};
5757
};
58-
static InternalHook g_internalHook;
58+
static InternalHook& GetInternalHook();
5959
};
6060

6161
class LazyD3D : public LazyDX

TFMain/DropDownHandler.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ LRESULT CALLBACK DropDownHandler::DropDownSubclassProc(HWND hWnd, UINT uMsg, WPA
7979
return 0;
8080
}
8181

82-
if (uMsg == HookHelper::TFM_ATTACH)
82+
if (uMsg == HookHelper::GetAttachMsg())
8383
{
8484
return 0;
8585
}
86-
if (uMsg == HookHelper::TFM_DETACH)
86+
if (uMsg == HookHelper::GetDetachMsg())
8787
{
8888
return 0;
8989
}
@@ -127,7 +127,7 @@ LRESULT CALLBACK DropDownHandler::ListviewpopupSubclassProc(HWND hWnd, UINT uMsg
127127
}
128128
}
129129

130-
if (uMsg == HookHelper::TFM_ATTACH)
130+
if (uMsg == HookHelper::GetAttachMsg())
131131
{
132132
HWND root{GetAncestor(hWnd, GA_ROOT)};
133133

@@ -155,7 +155,7 @@ LRESULT CALLBACK DropDownHandler::ListviewpopupSubclassProc(HWND hWnd, UINT uMsg
155155

156156
return 0;
157157
}
158-
if (uMsg == HookHelper::TFM_DETACH)
158+
if (uMsg == HookHelper::GetDetachMsg())
159159
{
160160
return 0;
161161
}

0 commit comments

Comments
 (0)