ReactOS 0.4.15-dev-8231-g29a56f3
usrapihk.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS user32.dll
4 * FILE: win32ss/user/user32/misc/usrapihk.c
5 * PURPOSE: User32.dll User32 Api Hook interface and support functions
6 * PROGRAMMER:
7 *
8 *
9 *
10 * Information:
11 * https://reactos.org/wiki/RegisterUserApiHook
12 *
13 */
14
15#include <user32.h>
16
18
31
32/* GLOBALS *******************************************************************/
33
40// API Hooked Message group bitmaps
44
45/* INTERNAL ******************************************************************/
46
47/*
48 Pre and Post Message handler stub.
49 */
53{
54 return 0;
55}
56
57/*
58 Check for API Hooked Message Overrides. Using message bitmapping.. One bit
59 corresponds to one message number.
60 */
61BOOL
63IsMsgOverride( UINT Msg, PUAHOWP puaowpOverride)
64{
65 UINT nMsg = Msg / 8; // Group Indexed, (Msg 1024) / 8 = (0 -> 127) bytes Max
66
67 if ( puaowpOverride && nMsg < puaowpOverride->Size )
68 {
69 return (puaowpOverride->MsgBitArray[nMsg] & (1 << (Msg & WM_SETFOCUS)));
70 }
71 return FALSE;
72}
73
74VOID
77{
79
80 if ( Src && Src->Size > 0 )
81 {
82 Dest->MsgBitArray = hkmsg;
83 nSize = Src->Size;
84 if ( Size < nSize) nSize = Size;
85 Dest->Size = nSize;
87 return;
88 }
89
90 Dest->MsgBitArray = NULL;
91 Dest->Size = 0;
92 return;
93}
94
95
96BOOL
99{
100 if ( ghmodUserApiHook && gfUserApiHook ) return TRUE;
101 return FALSE;
102}
103
104BOOL
107{
109 if (IsInsideUserApiHook()) return TRUE;
110
112 return FALSE;
113}
114
115BOOL
116WINAPI
118{
119 if ( ghmodUserApiHook == hInstance &&
121 {
124 return TRUE;
125 }
126 return FALSE;
127}
128
129VOID
132{
133 // Setup Structure.....
134 puah->size = sizeof(USERAPIHOOK);
138 puah->DefWndProcArray.Size = 0;
144 puah->PreWndProc = DefaultOWP;
145 puah->PostWndProc = DefaultOWP;
147 puah->WndProcArray.Size = 0;
151 puah->DlgProcArray.Size = 0;
160}
161
162BOOL
165{
167 USERAPIHOOKPROC pfn;
168 BOOL Ret = FALSE;
169
171 {
172 if ( !gcLoadUserApiHook )
173 {
175
176 pfn = gpfnInitUserApi;
180
182
183 if ( pfn ) Ret = pfn(uahStop, 0);
184
185 if ( hModule ) Ret = FreeLibrary(hModule);
186 }
187 }
188 return Ret;
189}
190
191BOOL
192WINAPI
194{
196 USERAPIHOOKPROC pfn = NULL, pfn1 = NULL;
197
201 {
202 pfn1 = gpfnInitUserApi;
203 if ( --gcLoadUserApiHook == 0 )
204 {
205 gfUserApiHook = 0;
207 if ( gcCallUserApiHook )
208 {
209 hInstance = NULL;
210 pfn1 = NULL;
211 pfn = gpfnInitUserApi;
213 }
214 else
215 {
219 }
220 }
221 }
223
224 if ( pfn )
225 {
226 pfn(uahShutdown, 0); // Shutdown.
227
229 pfn1 = gpfnInitUserApi;
230
231 if ( --gcLoadUserApiHook == 0 )
232 {
233 if ( gcCallUserApiHook )
234 {
235 hInstance = NULL;
236 pfn1 = NULL;
237 }
238 else
239 {
243 }
244 }
246 }
247
248 if ( pfn1 ) pfn1(uahStop, 0);
249
250 return hInstance != 0;
251}
252
253BOOL
254WINAPI
256{
257 USERAPIHOOK uah;
258
259 ResetUserApiHook(&uah);
260
262
263 if (!pfn(uahLoadInit,&uah) || // Swap data, User32 to and Uxtheme from!
265 uah.size <= 0 )
266 {
268 return FALSE;
269 }
270
271 if ( ghmodUserApiHook )
272 {
274 {
276 pfn(uahStop, 0);
277 return FALSE;
278 }
280 }
281 else
282 {
284 // Do not over write GetRealWindowOwner.
285 RtlCopyMemory(&guah, &uah, sizeof(USERAPIHOOK) - sizeof(LONG));
286 gpfnInitUserApi = pfn;
288 gfUserApiHook = 1;
289 // Copy Message Masks
291 &uah.DefWndProcArray,
293 sizeof(grgbDwpLiteHookMsg));
294
296 &uah.WndProcArray,
298 sizeof(grgbWndLiteHookMsg));
299
301 &uah.DlgProcArray,
303 sizeof(grgbDlgLiteHookMsg));
304 }
306 return TRUE;
307}
308
309BOOL
310WINAPI
312{
314}
315
316BOOL
317WINAPI
319{
320 BOOL Hook, Ret = FALSE;
321
323
325
326 /* Bypass SEH and go direct. */
327 if (!Hook) return RealMDIRedrawFrame(hWnd, flags);
328
330 {
332 }
334 {
335 ERR("Got exception in hooked MDIRedrawFrame!\n");
336 }
337 _SEH2_END;
338
340
341 return Ret;
342}
343
345{
346 sizeof(USERAPIHOOK),
349 {NULL, 0},
357 {NULL, 0},
360 {NULL, 0},
369};
370
371/* FUNCTIONS *****************************************************************/
372
373/*
374 * @implemented
375 */
377{
378 UNICODE_STRING m_dllname1;
379 UNICODE_STRING m_funname1;
380
381 if (puah->m_size == sizeof(USERAPIHOOKINFO))
382 {
383 WARN("RegisterUserApiHook: %S and %S\n", puah->m_dllname1, puah->m_funname1);
384 RtlInitUnicodeString(&m_dllname1, puah->m_dllname1);
385 RtlInitUnicodeString(&m_funname1, puah->m_funname1);
386 return NtUserRegisterUserApiHook( &m_dllname1, &m_funname1, 0, 0);
387 }
388 return FALSE;
389}
390
391/*
392 * @implemented
393 */
395{
397}
static HDC hDC
Definition: 3dtext.c:33
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
HWND hWnd
Definition: settings.c:17
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define WARN(fmt,...)
Definition: debug.h:115
#define ERR(fmt,...)
Definition: debug.h:113
HINSTANCE hInstance
Definition: charmap.c:19
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
struct @1643 Msg[]
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOLEAN NTAPI RtlIsThreadWithinLoaderCallout(VOID)
Definition: libsupp.c:347
UINT uFlags
Definition: api.c:59
HMODULE hModule
Definition: animate.c:44
#define FreeLibrary(x)
Definition: compat.h:748
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLbitfield flags
Definition: glext.h:7161
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
BOOL NTAPI NtUserRegisterUserApiHook(PUNICODE_STRING m_dllname1, PUNICODE_STRING m_funname1, DWORD dwUnknown3, DWORD dwUnknown4)
Definition: hook.c:1722
BOOL NTAPI NtUserUnregisterUserApiHook(VOID)
Definition: hook.c:1768
BOOL NTAPI NtUserDrawCaption(HWND hWnd, HDC hDc, LPCRECT lpRc, UINT uFlags)
Definition: painting.c:2456
INT NTAPI NtUserSetWindowRgn(HWND hWnd, HRGN hRgn, BOOL bRedraw)
Definition: winpos.c:3572
BOOL NTAPI NtUserEnableScrollBar(HWND hWnd, UINT wSBflags, UINT wArrows)
Definition: scrollbar.c:1242
static HDC
Definition: imagelist.c:92
unsigned int UINT
Definition: ndis.h:50
NTSYSAPI NTSTATUS NTAPI RtlEnterCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
NTSYSAPI NTSTATUS NTAPI RtlLeaveCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define FASTCALL
Definition: nt_native.h:50
EXTINLINE BOOL NtUserxMDIRedrawFrame(HWND hWnd)
Definition: ntwrapper.h:783
DWORD * PDWORD
Definition: pedump.c:68
long LONG
Definition: pedump.c:60
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
BYTE * MsgBitArray
Definition: undocuser.h:328
DWORD Size
Definition: undocuser.h:329
LPCWSTR m_funname1
Definition: undocuser.h:376
LPCWSTR m_dllname1
Definition: undocuser.h:375
SYSTEMPARAMETERSINFOA SystemParametersInfoA
Definition: undocuser.h:354
WNDPROC_OWP PostWndProc
Definition: undocuser.h:348
GETREALWINDOWOWNER GetRealWindowOwner
Definition: undocuser.h:360
FORCERESETUSERAPIHOOK ForceResetUserApiHook
Definition: undocuser.h:356
SETWINDOWRGN SetWindowRgn
Definition: undocuser.h:346
WNDPROC_OWP PreWndProc
Definition: undocuser.h:347
UAHOWP WndProcArray
Definition: undocuser.h:349
GETSCROLLINFO GetScrollInfo
Definition: undocuser.h:342
SYSTEMPARAMETERSINFOW SystemParametersInfoW
Definition: undocuser.h:355
GETSYSTEMMETRICS GetSystemMetrics
Definition: undocuser.h:353
DRAWFRAMECONTROL DrawFrameControl
Definition: undocuser.h:357
UAHOWP DlgProcArray
Definition: undocuser.h:352
UAHOWP DefWndProcArray
Definition: undocuser.h:341
MDIREDRAWFRAME MDIRedrawFrame
Definition: undocuser.h:359
ADJUSTWINDOWRECTEX AdjustWindowRectEx
Definition: undocuser.h:345
WNDPROC DefWindowProcA
Definition: undocuser.h:339
ENABLESCROLLBAR EnableScrollBar
Definition: undocuser.h:344
WNDPROC_OWP PreDefDlgProc
Definition: undocuser.h:350
WNDPROC_OWP PostDefDlgProc
Definition: undocuser.h:351
WNDPROC DefWindowProcW
Definition: undocuser.h:340
DRAWCAPTION DrawCaption
Definition: undocuser.h:358
SETSCROLLINFO SetScrollInfo
Definition: undocuser.h:343
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830
int32_t INT
Definition: typedefs.h:58
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOL(CALLBACK * USERAPIHOOKPROC)(UAPIHK State, PUSERAPIHOOK puah)
Definition: undocuser.h:370
@ uahStop
Definition: undocuser.h:366
@ uahLoadInit
Definition: undocuser.h:365
@ uahShutdown
Definition: undocuser.h:367
struct tagUSERAPIHOOK USERAPIHOOK
static __inline void LoadUserApiHook()
Definition: user_x.h:85
LRESULT WINAPI RealDefWindowProcW(HWND, UINT, WPARAM, LPARAM)
Definition: defwnd.c:1037
RTL_CRITICAL_SECTION gcsUserApiHook
Definition: usrapihk.c:39
BOOL WINAPI RealGetScrollInfo(HWND, INT, LPSCROLLINFO)
Definition: scrollbar.c:1512
HINSTANCE ghmodUserApiHook
Definition: usrapihk.c:37
LRESULT WINAPI RealDefWindowProcA(HWND, UINT, WPARAM, LPARAM)
Definition: defwnd.c:813
INT WINAPI RealSetScrollInfo(HWND, int, LPCSCROLLINFO, BOOL)
Definition: scrollbar.c:1665
BOOL FASTCALL EndUserApiHook(VOID)
Definition: usrapihk.c:164
BOOL FASTCALL IsMsgOverride(UINT Msg, PUAHOWP puaowpOverride)
Definition: usrapihk.c:63
BOOL WINAPI ForceResetUserApiHook(HINSTANCE hInstance)
Definition: usrapihk.c:117
BYTE grgbWndLiteHookMsg[128]
Definition: usrapihk.c:42
BOOL FASTCALL BeginIfHookedUserApiHook(VOID)
Definition: usrapihk.c:106
BOOL WINAPI InitUserApiHook(HINSTANCE hInstance, USERAPIHOOKPROC pfn)
Definition: usrapihk.c:255
VOID FASTCALL ResetUserApiHook(PUSERAPIHOOK puah)
Definition: usrapihk.c:131
BYTE grgbDwpLiteHookMsg[128]
Definition: usrapihk.c:41
BOOL WINAPI MDIRedrawFrame(HWND hWnd, DWORD flags)
Definition: usrapihk.c:318
BOOL FASTCALL IsInsideUserApiHook(VOID)
Definition: usrapihk.c:98
int WINAPI RealGetSystemMetrics(int)
Definition: desktop.c:128
BOOL WINAPI UnregisterUserApiHook(VOID)
Definition: usrapihk.c:394
BOOL WINAPI RealMDIRedrawFrame(HWND, DWORD)
Definition: usrapihk.c:311
USERAPIHOOK guah
Definition: usrapihk.c:344
LRESULT WINAPI DefaultOWP(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR lResult, PDWORD pData)
Definition: usrapihk.c:52
DWORD WINAPI GetRealWindowOwner(HWND)
Definition: window.c:1899
BOOL WINAPI RealDrawFrameControl(HDC, LPRECT, UINT, UINT)
Definition: draw.c:1423
BYTE grgbDlgLiteHookMsg[128]
Definition: usrapihk.c:43
BOOL WINAPI RegisterUserApiHook(PUSERAPIHOOKINFO puah)
Definition: usrapihk.c:376
BOOL WINAPI RealSystemParametersInfoW(UINT, UINT, PVOID, UINT)
Definition: desktop.c:341
BOOL WINAPI RealSystemParametersInfoA(UINT, UINT, PVOID, UINT)
Definition: desktop.c:180
DWORD gfUserApiHook
Definition: usrapihk.c:36
LONG gcCallUserApiHook
Definition: usrapihk.c:35
VOID FASTCALL CopyMsgMask(PUAHOWP Dest, PUAHOWP Src, PVOID hkmsg, DWORD Size)
Definition: usrapihk.c:76
DWORD gcLoadUserApiHook
Definition: usrapihk.c:34
BOOL WINAPI RealUserDrawCaption(HWND hWnd, HDC hDC, LPCRECT lpRc, UINT uFlags)
Definition: nonclient.c:80
USERAPIHOOKPROC gpfnInitUserApi
Definition: usrapihk.c:38
BOOL WINAPI ClearUserApiHook(HINSTANCE hInstance)
Definition: usrapihk.c:193
BOOL WINAPI RealAdjustWindowRectEx(LPRECT, DWORD, BOOL, DWORD)
Definition: nonclient.c:90
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2084
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define WINAPI
Definition: msvc.h:6
#define WM_SETFOCUS
Definition: winuser.h:1613
unsigned char BYTE
Definition: xxhash.c:193