ReactOS  0.4.14-dev-552-g2fad488
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  * http://www.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  */
50 LRESULT
51 WINAPI
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  */
61 BOOL
63 IsMsgOverride( 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 
74 VOID
77 {
78  DWORD nSize;
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 
96 BOOL
99 {
100  if ( ghmodUserApiHook && gfUserApiHook ) return TRUE;
101  return FALSE;
102 }
103 
104 BOOL
105 FASTCALL
107 {
109  if (IsInsideUserApiHook()) return TRUE;
110 
112  return FALSE;
113 }
114 
115 BOOL
116 WINAPI
118 {
119  if ( ghmodUserApiHook == hInstance &&
121  {
124  return TRUE;
125  }
126  return FALSE;
127 }
128 
129 VOID
130 FASTCALL
132 {
133  // Setup Structure.....
134  puah->size = sizeof(USERAPIHOOK);
138  puah->DefWndProcArray.Size = 0;
144  puah->PreWndProc = DefaultOWP;
145  puah->PostWndProc = DefaultOWP;
146  puah->WndProcArray.MsgBitArray = NULL;
147  puah->WndProcArray.Size = 0;
148  puah->PreDefDlgProc = DefaultOWP;
149  puah->PostDefDlgProc = DefaultOWP;
150  puah->DlgProcArray.MsgBitArray = NULL;
151  puah->DlgProcArray.Size = 0;
160 }
161 
162 BOOL
163 FASTCALL
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 
191 BOOL
192 WINAPI
194 {
196  USERAPIHOOKPROC pfn = NULL, pfn1 = NULL;
197 
200  if ( ghmodUserApiHook == hInstance )
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;
212  gcLoadUserApiHook = 1;
213  }
214  else
215  {
216  hInstance = hModule;
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 
253 BOOL
254 WINAPI
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  {
273  if ( ghmodUserApiHook != hInstance )
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;
287  gcLoadUserApiHook = 1;
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 
309 BOOL
310 WINAPI
312 {
313  return NtUserxMDIRedrawFrame(hWnd);
314 }
315 
316 BOOL
317 WINAPI
319 {
320  BOOL Hook, Ret = FALSE;
321 
322  LoadUserApiHook();
323 
324  Hook = BeginIfHookedUserApiHook();
325 
326  /* Bypass SEH and go direct. */
327  if (!Hook) return RealMDIRedrawFrame(hWnd, flags);
328 
329  _SEH2_TRY
330  {
331  Ret = guah.MDIRedrawFrame(hWnd, flags);
332  }
334  {
335  ERR("Got exception in hooked MDIRedrawFrame!\n");
336  }
337  _SEH2_END;
338 
339  EndUserApiHook();
340 
341  return Ret;
342 }
343 
345 {
346  sizeof(USERAPIHOOK),
349  {NULL, 0},
355  DefaultOWP,
356  DefaultOWP,
357  {NULL, 0},
358  DefaultOWP,
359  DefaultOWP,
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",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 }
struct tagUSERAPIHOOK USERAPIHOOK
BOOL NTAPI NtUserRegisterUserApiHook(PUNICODE_STRING m_dllname1, PUNICODE_STRING m_funname1, DWORD dwUnknown3, DWORD dwUnknown4)
Definition: hook.c:1724
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
BOOL WINAPI RealMDIRedrawFrame(HWND, DWORD)
Definition: usrapihk.c:311
VOID FASTCALL ResetUserApiHook(PUSERAPIHOOK puah)
Definition: usrapihk.c:131
BOOL WINAPI MDIRedrawFrame(HWND hWnd, DWORD flags)
Definition: usrapihk.c:318
GETREALWINDOWOWNER GetRealWindowOwner
Definition: undocuser.h:352
WNDPROC_OWP PostDefDlgProc
Definition: undocuser.h:343
UAHOWP DlgProcArray
Definition: undocuser.h:344
EXTINLINE BOOL NtUserxMDIRedrawFrame(HWND hWnd)
Definition: ntwrapper.h:783
HINSTANCE ghmodUserApiHook
Definition: usrapihk.c:37
#define WARN(fmt,...)
Definition: debug.h:111
struct @1591 Msg[]
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
BOOL WINAPI RealSystemParametersInfoW(UINT, UINT, PVOID, UINT)
Definition: desktop.c:341
static HDC
Definition: imagelist.c:92
HWND hWnd
Definition: settings.c:17
VOID FASTCALL CopyMsgMask(PUAHOWP Dest, PUAHOWP Src, PVOID hkmsg, DWORD Size)
Definition: usrapihk.c:76
NTSYSAPI NTSTATUS NTAPI RtlEnterCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
BOOL WINAPI RealUserDrawCaption(HWND hWnd, HDC hDC, LPCRECT lpRc, UINT uFlags)
Definition: nonclient.c:80
UINT_PTR WPARAM
Definition: windef.h:207
BOOL FASTCALL BeginIfHookedUserApiHook(VOID)
Definition: usrapihk.c:106
BOOL FASTCALL IsMsgOverride(UINT Msg, PUAHOWP puaowpOverride)
Definition: usrapihk.c:63
BOOL WINAPI UnregisterUserApiHook(VOID)
Definition: usrapihk.c:394
UINT uFlags
Definition: api.c:59
#define FASTCALL
Definition: nt_native.h:50
LONG gcCallUserApiHook
Definition: usrapihk.c:35
BOOL WINAPI RegisterUserApiHook(PUSERAPIHOOKINFO puah)
Definition: usrapihk.c:376
int32_t INT
Definition: typedefs.h:56
BOOL NTAPI NtUserEnableScrollBar(HWND hWnd, UINT wSBflags, UINT wArrows)
Definition: scrollbar.c:1282
WPARAM wParam
Definition: combotst.c:138
_SEH2_TRY
Definition: create.c:4250
uint32_t ULONG_PTR
Definition: typedefs.h:63
DRAWCAPTION DrawCaption
Definition: undocuser.h:350
BOOL(CALLBACK * USERAPIHOOKPROC)(UAPIHK State, PUSERAPIHOOK puah)
Definition: undocuser.h:362
HINSTANCE hInstance
Definition: charmap.c:20
SETWINDOWRGN SetWindowRgn
Definition: undocuser.h:338
LPCWSTR m_funname1
Definition: undocuser.h:368
ADJUSTWINDOWRECTEX AdjustWindowRectEx
Definition: undocuser.h:337
WNDPROC DefWindowProcW
Definition: undocuser.h:332
WNDPROC_OWP PostWndProc
Definition: undocuser.h:340
WINE_DEFAULT_DEBUG_CHANNEL(user32)
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
ENABLESCROLLBAR EnableScrollBar
Definition: undocuser.h:336
NTSYSAPI NTSTATUS NTAPI RtlLeaveCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
INT WINAPI RealSetScrollInfo(HWND, int, LPCSCROLLINFO, BOOL)
Definition: scrollbar.c:1632
SYSTEMPARAMETERSINFOW SystemParametersInfoW
Definition: undocuser.h:347
UAHOWP DefWndProcArray
Definition: undocuser.h:333
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
MDIREDRAWFRAME MDIRedrawFrame
Definition: undocuser.h:351
BOOL FASTCALL IsInsideUserApiHook(VOID)
Definition: usrapihk.c:98
GETSCROLLINFO GetScrollInfo
Definition: undocuser.h:334
LRESULT WINAPI DefaultOWP(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR lResult, PDWORD pData)
Definition: usrapihk.c:52
LPCWSTR m_dllname1
Definition: undocuser.h:367
BOOL FASTCALL EndUserApiHook(VOID)
Definition: usrapihk.c:164
BOOL WINAPI ClearUserApiHook(HINSTANCE hInstance)
Definition: usrapihk.c:193
GETSYSTEMMETRICS GetSystemMetrics
Definition: undocuser.h:345
#define FreeLibrary(x)
Definition: compat.h:413
BOOL WINAPI RealGetScrollInfo(HWND, INT, LPSCROLLINFO)
Definition: scrollbar.c:1479
DRAWFRAMECONTROL DrawFrameControl
Definition: undocuser.h:349
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD Size
Definition: undocuser.h:321
FORCERESETUSERAPIHOOK ForceResetUserApiHook
Definition: undocuser.h:348
WNDPROC_OWP PreDefDlgProc
Definition: undocuser.h:342
GLbitfield flags
Definition: glext.h:7161
WNDPROC DefWindowProcA
Definition: undocuser.h:331
BOOL WINAPI InitUserApiHook(HINSTANCE hInstance, USERAPIHOOKPROC pfn)
Definition: usrapihk.c:255
#define InterlockedDecrement
Definition: armddk.h:52
BOOL WINAPI RealAdjustWindowRectEx(LPRECT, DWORD, BOOL, DWORD)
Definition: nonclient.c:90
unsigned char BYTE
Definition: mem.h:68
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
BYTE * MsgBitArray
Definition: undocuser.h:320
#define ERR(fmt,...)
Definition: debug.h:109
static HDC hDC
Definition: 3dtext.c:33
DWORD gcLoadUserApiHook
Definition: usrapihk.c:34
_SEH2_END
Definition: create.c:4424
#define InterlockedIncrement
Definition: armddk.h:53
SYSTEMPARAMETERSINFOA SystemParametersInfoA
Definition: undocuser.h:346
BOOL WINAPI RealDrawFrameControl(HDC, LPRECT, UINT, UINT)
Definition: draw.c:1419
BYTE grgbDlgLiteHookMsg[128]
Definition: usrapihk.c:43
int WINAPI RealGetSystemMetrics(int)
Definition: desktop.c:128
static __inline void LoadUserApiHook()
Definition: user_x.h:85
BOOLEAN NTAPI RtlIsThreadWithinLoaderCallout(VOID)
Definition: libsupp.c:348
WNDPROC_OWP PreWndProc
Definition: undocuser.h:339
unsigned int UINT
Definition: ndis.h:50
UAHOWP WndProcArray
Definition: undocuser.h:341
DWORD * PDWORD
Definition: pedump.c:68
USERAPIHOOK guah
Definition: usrapihk.c:344
BOOL WINAPI ForceResetUserApiHook(HINSTANCE hInstance)
Definition: usrapihk.c:117
BYTE grgbWndLiteHookMsg[128]
Definition: usrapihk.c:42
BYTE grgbDwpLiteHookMsg[128]
Definition: usrapihk.c:41
BOOL WINAPI RealSystemParametersInfoA(UINT, UINT, PVOID, UINT)
Definition: desktop.c:180
BOOL NTAPI NtUserUnregisterUserApiHook(VOID)
Definition: hook.c:1770
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define WM_SETFOCUS
Definition: winuser.h:1595
DWORD WINAPI GetRealWindowOwner(HWND)
Definition: window.c:1911
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
LONG_PTR LRESULT
Definition: windef.h:209
LRESULT WINAPI RealDefWindowProcA(HWND, UINT, WPARAM, LPARAM)
Definition: defwnd.c:806
RTL_CRITICAL_SECTION gcsUserApiHook
Definition: usrapihk.c:39
LRESULT WINAPI RealDefWindowProcW(HWND, UINT, WPARAM, LPARAM)
Definition: defwnd.c:1014
INT NTAPI NtUserSetWindowRgn(HWND hWnd, HRGN hRgn, BOOL bRedraw)
Definition: winpos.c:3475
LPARAM lParam
Definition: combotst.c:139
USERAPIHOOKPROC gpfnInitUserApi
Definition: usrapihk.c:38
DWORD gfUserApiHook
Definition: usrapihk.c:36
BOOL NTAPI NtUserDrawCaption(HWND hWnd, HDC hDc, LPCRECT lpRc, UINT uFlags)
Definition: painting.c:2447
SETSCROLLINFO SetScrollInfo
Definition: undocuser.h:335
HMODULE hModule
Definition: animate.c:44