@@ -48,43 +48,205 @@ void* GetIATFunctionAddress(BYTE* base, const char* dll_name, const char* search
4848 }
4949 return nullptr ;
5050}
51+
52+
53+ namespace FunctionHooks
54+ {
55+
56+ HWND WINAPI HookCreateWindowExA (
57+ DWORD dwExStyle,
58+ LPCSTR lpClassName,
59+ LPCSTR lpWindowName,
60+ DWORD dwStyle,
61+ int X,
62+ int Y,
63+ int nWidth,
64+ int nHeight,
65+ HWND hWndParent,
66+ HMENU hMenu,
67+ HINSTANCE hInstance,
68+ LPVOID lpParam
69+ )
70+ {
71+ if (!hWndParent)
72+ {
73+ // We assume this is the main window
74+ dwStyle &= ~WS_OVERLAPPEDWINDOW;
75+ dwExStyle &= ~(WS_EX_OVERLAPPEDWINDOW | WS_EX_TOPMOST);
76+ auto result = CreateWindowExA (dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y,
77+ nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
78+ if (!mainWindow) mainWindow = result;
79+ return result;
80+ }
81+ else return CreateWindowExA (dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y,
82+ nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
83+ }
84+
85+ LONG_PTR WINAPI HookSetWindowLongPtrA (
86+ HWND hWnd,
87+ int nIndex,
88+ LONG dwNewLong)
89+ {
90+ if (hWnd == mainWindow)
91+ {
92+ switch (nIndex)
93+ {
94+ case GWL_STYLE:
95+ dwNewLong &= ~WS_OVERLAPPEDWINDOW;
96+ break ;
97+ case GWL_EXSTYLE:
98+ dwNewLong &= ~(WS_EX_OVERLAPPEDWINDOW | WS_EX_TOPMOST);
99+ break ;
100+ default :
101+
102+ break ;
103+ }
104+ }
105+ return SetWindowLongPtrA (hWnd, nIndex, dwNewLong);
106+ }
107+
108+ HWND WINAPI HookCreateWindowExW (
109+ DWORD dwExStyle,
110+ LPCWSTR lpClassName,
111+ LPCWSTR lpWindowName,
112+ DWORD dwStyle,
113+ int X,
114+ int Y,
115+ int nWidth,
116+ int nHeight,
117+ HWND hWndParent,
118+ HMENU hMenu,
119+ HINSTANCE hInstance,
120+ LPVOID lpParam
121+ )
122+ {
123+ if (!hWndParent)
124+ {
125+ // We assume this is the main window
126+ dwStyle &= ~WS_OVERLAPPEDWINDOW;
127+ dwExStyle &= ~(WS_EX_OVERLAPPEDWINDOW | WS_EX_TOPMOST);
128+ auto result = CreateWindowExW (dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y,
129+ nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
130+ if (!mainWindow || !::IsWindow (mainWindow)) mainWindow = result;
131+ return result;
132+ }
133+ else return CreateWindowExW (dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y,
134+ nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
135+ }
136+ LONG_PTR WINAPI HookSetWindowLongPtrW (
137+ HWND hWnd,
138+ int nIndex,
139+ LONG dwNewLong)
140+ {
141+ if (hWnd == mainWindow)
142+ {
143+ switch (nIndex)
144+ {
145+ case GWL_STYLE:
146+ dwNewLong &= ~WS_OVERLAPPEDWINDOW;
147+ break ;
148+ case GWL_EXSTYLE:
149+ dwNewLong &= ~(WS_EX_OVERLAPPEDWINDOW | WS_EX_TOPMOST);
150+ break ;
151+ default :
152+
153+ break ;
154+ }
155+ }
156+ return SetWindowLongPtrW (hWnd, nIndex, dwNewLong);
157+ }
158+ BOOL WINAPI HookSetWindowPos (
159+ HWND hWnd,
160+ HWND hWndInsertAfter,
161+ int X,
162+ int Y,
163+ int cx,
164+ int cy,
165+ UINT uFlags
166+ ) {
167+ if (hWnd == mainWindow)
168+ {
169+ if (BorderlessSettings::WindowModeOverride == 1 ) hWndInsertAfter = HWND_TOPMOST;
170+ else if (BorderlessSettings::WindowModeOverride == 2 ) hWndInsertAfter = HWND_TOP;
171+ if (BorderlessSettings::EnableCustomWindowParameters)
172+ {
173+ cx = BorderlessSettings::CustomWindowParameters::CustomWidth;
174+ cy = BorderlessSettings::CustomWindowParameters::CustomHeight;
175+ X = BorderlessSettings::CustomWindowParameters::CustomPositionX;
176+ Y = BorderlessSettings::CustomWindowParameters::CustomPositionY;
177+ }
178+ }
179+
180+ return SetWindowPos (hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
181+ }
182+ BOOL WINAPI HookMoveWindow (
183+ HWND hWnd,
184+ int X,
185+ int Y,
186+ int nWidth,
187+ int nHeight,
188+ BOOL bRepaint
189+ ) {
190+
191+ if (hWnd == mainWindow && BorderlessSettings::EnableCustomWindowParameters)
192+ {
193+ nWidth = BorderlessSettings::CustomWindowParameters::CustomWidth;
194+ nHeight = BorderlessSettings::CustomWindowParameters::CustomHeight;
195+ X = BorderlessSettings::CustomWindowParameters::CustomPositionX;
196+ Y = BorderlessSettings::CustomWindowParameters::CustomPositionY;
197+ }
198+ return ::MoveWindow (hWnd, X, Y, nWidth, nHeight, bRepaint);
199+
200+ }
201+ }
202+
203+
204+
51205// In the DLLMain, we create our hooks
52206BOOL WINAPI DllMain (HINSTANCE hInstance, DWORD dwReason, LPVOID reserved)
53207{
54-
208+ if (!BorderlessSettings::settingsSetUpComplete) SetUpDLLIniDir (hInstance);
209+ if (!BorderlessSettings::Enabled) return FALSE ;
55210 // Process attach is where our library gets attached to the process
56211 // Therefore we create our hooks here
57- if (dwReason == DLL_PROCESS_ATTACH) {
58-
59-
212+ if (dwReason == DLL_PROCESS_ATTACH) {
60213 // Since we do not need the thread library calls provided by the runtime, we disable them for our module
61- DisableThreadLibraryCalls ((HMODULE)hInstance);
214+ DisableThreadLibraryCalls ((HMODULE)hInstance);
62215 // In these calls, we detour the original functions to our desired code paths.
63216 if (auto CreateWindowIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " CreateWindowExW" ))
64217 {
65218 PatchAddressPointer ((uintptr_t )CreateWindowIATAddress, (uintptr_t )&FunctionHooks::HookCreateWindowExW);
66- void * SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrW" );
67- if (!SetWindowAddress) SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongW" );
68- if (SetWindowAddress)
219+ void * SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrW" );
220+ if (!SetWindowLPtrIATAddress) SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongW" );
221+ if (SetWindowLPtrIATAddress)
69222 {
70- PatchAddressPointer ((uintptr_t )SetWindowAddress , (uintptr_t )&FunctionHooks::HookSetWindowLongPtrW);
223+ PatchAddressPointer ((uintptr_t )SetWindowLPtrIATAddress , (uintptr_t )&FunctionHooks::HookSetWindowLongPtrW);
71224 }
225+
72226 }
73227 if (auto CreateWindowIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " CreateWindowExA" ))
74228 {
75229 PatchAddressPointer ((uintptr_t )CreateWindowIATAddress, (uintptr_t )&FunctionHooks::HookCreateWindowExA);
76- void * SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrA" );
77- if (!SetWindowAddress) SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongA" );
78- if (SetWindowAddress )
230+ void * SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrA" );
231+ if (!SetWindowLPtrIATAddress) SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongA" );
232+ if (SetWindowLPtrIATAddress )
79233 {
80- PatchAddressPointer ((uintptr_t )SetWindowAddress , (uintptr_t )&FunctionHooks::HookSetWindowLongPtrA);
234+ PatchAddressPointer ((uintptr_t )SetWindowLPtrIATAddress , (uintptr_t )&FunctionHooks::HookSetWindowLongPtrA);
81235 }
82236
83237 }
84- }
238+ if (auto SetWindowsPosIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowPos" ))
239+ {
240+ PatchAddressPointer ((uintptr_t )SetWindowsPosIATAddress, (uintptr_t )FunctionHooks::HookSetWindowPos);
241+ }
242+ if (auto MoveWindowIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " MoveWindow" ))
243+ {
244+ PatchAddressPointer ((uintptr_t )MoveWindowIATAddress, (uintptr_t )FunctionHooks::HookMoveWindow);
245+ }
246+ }
85247 // Detach message, which is where the process unloads all our changes.
86248 // Here, we return everything to its original state
87- else if (dwReason == DLL_PROCESS_DETACH) {
249+ else if (dwReason == DLL_PROCESS_DETACH) {
88250 if (auto CreateWindowIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " CreateWindowExW" ))
89251 {
90252 PatchAddressPointer ((uintptr_t )CreateWindowIATAddress, (uintptr_t )::CreateWindowExW);
@@ -93,22 +255,25 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID reserved)
93255 {
94256 PatchAddressPointer ((uintptr_t )CreateWindowIATAddress, (uintptr_t )::CreateWindowExA);
95257 }
96- void * SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrW" );
97- if (!SetWindowAddress) SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongW" );
98- if (SetWindowAddress)
258+ void * SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrW" );
259+ if (!SetWindowLPtrIATAddress) SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongW" );
260+ if (SetWindowLPtrIATAddress)
261+ {
262+ PatchAddressPointer ((uintptr_t )SetWindowLPtrIATAddress, (uintptr_t )SetWindowLongPtrW);
263+ }
264+
265+
266+ SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrA" );
267+ if (!SetWindowLPtrIATAddress) SetWindowLPtrIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongA" );
268+ if (SetWindowLPtrIATAddress)
99269 {
100- PatchAddressPointer ((uintptr_t )SetWindowAddress , (uintptr_t )SetWindowLongPtrW );
270+ PatchAddressPointer ((uintptr_t )SetWindowLPtrIATAddress , (uintptr_t )SetWindowLongPtrA );
101271 }
102-
103-
104- SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongPtrA" );
105- if (!SetWindowAddress) SetWindowAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowLongA" );
106- if (SetWindowAddress)
272+ if (auto SetWindowsPosIATAddress = GetIATFunctionAddress ((BYTE*)GetModuleHandle (NULL ), " USER32.dll" , " SetWindowPos" ))
107273 {
108- PatchAddressPointer ((uintptr_t )SetWindowAddress , (uintptr_t )SetWindowLongPtrA );
274+ PatchAddressPointer ((uintptr_t )SetWindowsPosIATAddress , (uintptr_t )::SetWindowPos );
109275 }
110-
111276
112- }
113- return TRUE ;
277+ }
278+ return TRUE ;
114279}
0 commit comments