2009-09-15 15:24:09 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS user32.dll
|
2015-11-01 09:41:29 +00:00
|
|
|
* FILE: win32ss/user/user32/misc/usrapihk.c
|
2009-09-15 15:24:09 +00:00
|
|
|
* PURPOSE: User32.dll User32 Api Hook interface and support functions
|
|
|
|
* PROGRAMMER:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Information:
|
2023-02-06 14:01:52 +00:00
|
|
|
* https://reactos.org/wiki/RegisterUserApiHook
|
2009-09-15 15:24:09 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-12-23 22:42:40 +00:00
|
|
|
#include <user32.h>
|
2009-09-15 15:24:09 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(user32);
|
|
|
|
|
|
|
|
BOOL WINAPI RealAdjustWindowRectEx(LPRECT,DWORD,BOOL,DWORD);
|
|
|
|
LRESULT WINAPI RealDefWindowProcA(HWND,UINT,WPARAM,LPARAM);
|
|
|
|
LRESULT WINAPI RealDefWindowProcW(HWND,UINT,WPARAM,LPARAM);
|
|
|
|
BOOL WINAPI RealDrawFrameControl(HDC,LPRECT,UINT,UINT);
|
|
|
|
BOOL WINAPI RealGetScrollInfo(HWND,INT,LPSCROLLINFO);
|
|
|
|
int WINAPI RealGetSystemMetrics(int);
|
|
|
|
BOOL WINAPI RealMDIRedrawFrame(HWND,DWORD);
|
|
|
|
INT WINAPI RealSetScrollInfo(HWND,int,LPCSCROLLINFO,BOOL);
|
|
|
|
BOOL WINAPI RealSystemParametersInfoA(UINT,UINT,PVOID,UINT);
|
|
|
|
BOOL WINAPI RealSystemParametersInfoW(UINT,UINT,PVOID,UINT);
|
2011-08-25 08:51:26 +00:00
|
|
|
DWORD WINAPI GetRealWindowOwner(HWND);
|
2017-02-20 17:31:50 +00:00
|
|
|
BOOL WINAPI RealUserDrawCaption(HWND hWnd, HDC hDC, LPCRECT lpRc, UINT uFlags);
|
2009-09-15 15:24:09 +00:00
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
|
|
|
DWORD gcLoadUserApiHook = 0;
|
|
|
|
LONG gcCallUserApiHook = 0;
|
2015-09-01 22:10:13 +00:00
|
|
|
DWORD gfUserApiHook = 0;
|
2009-09-15 15:24:09 +00:00
|
|
|
HINSTANCE ghmodUserApiHook = NULL;
|
|
|
|
USERAPIHOOKPROC gpfnInitUserApi;
|
|
|
|
RTL_CRITICAL_SECTION gcsUserApiHook;
|
|
|
|
// API Hooked Message group bitmaps
|
|
|
|
BYTE grgbDwpLiteHookMsg[128];
|
|
|
|
BYTE grgbWndLiteHookMsg[128];
|
|
|
|
BYTE grgbDlgLiteHookMsg[128];
|
|
|
|
|
|
|
|
/* INTERNAL ******************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pre and Post Message handler stub.
|
|
|
|
*/
|
|
|
|
LRESULT
|
|
|
|
WINAPI
|
|
|
|
DefaultOWP(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR lResult, PDWORD pData)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check for API Hooked Message Overrides. Using message bitmapping.. One bit
|
|
|
|
corresponds to one message number.
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
FASTCALL
|
|
|
|
IsMsgOverride( UINT Msg, PUAHOWP puaowpOverride)
|
|
|
|
{
|
|
|
|
UINT nMsg = Msg / 8; // Group Indexed, (Msg 1024) / 8 = (0 -> 127) bytes Max
|
|
|
|
|
|
|
|
if ( puaowpOverride && nMsg < puaowpOverride->Size )
|
|
|
|
{
|
|
|
|
return (puaowpOverride->MsgBitArray[nMsg] & (1 << (Msg & WM_SETFOCUS)));
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
CopyMsgMask(PUAHOWP Dest, PUAHOWP Src, PVOID hkmsg, DWORD Size)
|
|
|
|
{
|
|
|
|
DWORD nSize;
|
|
|
|
|
|
|
|
if ( Src && Src->Size > 0 )
|
|
|
|
{
|
|
|
|
Dest->MsgBitArray = hkmsg;
|
|
|
|
nSize = Src->Size;
|
|
|
|
if ( Size < nSize) nSize = Size;
|
|
|
|
Dest->Size = nSize;
|
|
|
|
RtlCopyMemory(Dest->MsgBitArray, Src->MsgBitArray, nSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dest->MsgBitArray = NULL;
|
|
|
|
Dest->Size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
FASTCALL
|
|
|
|
IsInsideUserApiHook(VOID)
|
|
|
|
{
|
|
|
|
if ( ghmodUserApiHook && gfUserApiHook ) return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
FASTCALL
|
|
|
|
BeginIfHookedUserApiHook(VOID)
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&gcCallUserApiHook);
|
|
|
|
if (IsInsideUserApiHook()) return TRUE;
|
|
|
|
|
|
|
|
InterlockedDecrement(&gcCallUserApiHook);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2018-03-09 08:06:08 +00:00
|
|
|
WINAPI
|
2009-09-15 15:24:09 +00:00
|
|
|
ForceResetUserApiHook(HINSTANCE hInstance)
|
|
|
|
{
|
|
|
|
if ( ghmodUserApiHook == hInstance &&
|
|
|
|
RtlIsThreadWithinLoaderCallout() )
|
|
|
|
{
|
|
|
|
ResetUserApiHook(&guah);
|
|
|
|
gpfnInitUserApi = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
ResetUserApiHook(PUSERAPIHOOK puah)
|
|
|
|
{
|
|
|
|
// Setup Structure.....
|
|
|
|
puah->size = sizeof(USERAPIHOOK);
|
2013-11-17 22:10:42 +00:00
|
|
|
puah->DefWindowProcA = RealDefWindowProcA;
|
|
|
|
puah->DefWindowProcW = RealDefWindowProcW;
|
2009-09-15 15:24:09 +00:00
|
|
|
puah->DefWndProcArray.MsgBitArray = NULL;
|
|
|
|
puah->DefWndProcArray.Size = 0;
|
2017-02-20 17:31:50 +00:00
|
|
|
puah->GetScrollInfo = RealGetScrollInfo;
|
|
|
|
puah->SetScrollInfo = RealSetScrollInfo;
|
|
|
|
puah->EnableScrollBar = NtUserEnableScrollBar;
|
|
|
|
puah->AdjustWindowRectEx = RealAdjustWindowRectEx;
|
2013-11-17 22:10:42 +00:00
|
|
|
puah->SetWindowRgn = NtUserSetWindowRgn;
|
|
|
|
puah->PreWndProc = DefaultOWP;
|
|
|
|
puah->PostWndProc = DefaultOWP;
|
2009-09-15 15:24:09 +00:00
|
|
|
puah->WndProcArray.MsgBitArray = NULL;
|
|
|
|
puah->WndProcArray.Size = 0;
|
2013-11-17 22:10:42 +00:00
|
|
|
puah->PreDefDlgProc = DefaultOWP;
|
|
|
|
puah->PostDefDlgProc = DefaultOWP;
|
2009-09-15 15:24:09 +00:00
|
|
|
puah->DlgProcArray.MsgBitArray = NULL;
|
|
|
|
puah->DlgProcArray.Size = 0;
|
2017-02-20 17:31:50 +00:00
|
|
|
puah->GetSystemMetrics = RealGetSystemMetrics;
|
|
|
|
puah->SystemParametersInfoA = RealSystemParametersInfoA;
|
|
|
|
puah->SystemParametersInfoW = RealSystemParametersInfoW;
|
|
|
|
puah->ForceResetUserApiHook = ForceResetUserApiHook;
|
|
|
|
puah->DrawFrameControl = RealDrawFrameControl;
|
|
|
|
puah->DrawCaption = RealUserDrawCaption;
|
|
|
|
puah->MDIRedrawFrame = RealMDIRedrawFrame;
|
|
|
|
puah->GetRealWindowOwner = GetRealWindowOwner;
|
2009-09-15 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
FASTCALL
|
|
|
|
EndUserApiHook(VOID)
|
|
|
|
{
|
|
|
|
HMODULE hModule;
|
|
|
|
USERAPIHOOKPROC pfn;
|
|
|
|
BOOL Ret = FALSE;
|
|
|
|
|
|
|
|
if ( !InterlockedDecrement(&gcCallUserApiHook) )
|
|
|
|
{
|
|
|
|
if ( !gcLoadUserApiHook )
|
|
|
|
{
|
|
|
|
RtlEnterCriticalSection(&gcsUserApiHook);
|
|
|
|
|
|
|
|
pfn = gpfnInitUserApi;
|
|
|
|
hModule = ghmodUserApiHook;
|
|
|
|
ghmodUserApiHook = NULL;
|
|
|
|
gpfnInitUserApi = NULL;
|
|
|
|
|
|
|
|
RtlLeaveCriticalSection(&gcsUserApiHook);
|
|
|
|
|
|
|
|
if ( pfn ) Ret = pfn(uahStop, 0);
|
|
|
|
|
|
|
|
if ( hModule ) Ret = FreeLibrary(hModule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
ClearUserApiHook(HINSTANCE hInstance)
|
|
|
|
{
|
|
|
|
HMODULE hModule;
|
|
|
|
USERAPIHOOKPROC pfn = NULL, pfn1 = NULL;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection(&gcsUserApiHook);
|
|
|
|
hModule = ghmodUserApiHook;
|
|
|
|
if ( ghmodUserApiHook == hInstance )
|
|
|
|
{
|
|
|
|
pfn1 = gpfnInitUserApi;
|
2011-09-19 12:43:44 +00:00
|
|
|
if ( --gcLoadUserApiHook == 0 )
|
2009-09-15 15:24:09 +00:00
|
|
|
{
|
|
|
|
gfUserApiHook = 0;
|
|
|
|
ResetUserApiHook(&guah);
|
|
|
|
if ( gcCallUserApiHook )
|
|
|
|
{
|
|
|
|
hInstance = NULL;
|
|
|
|
pfn1 = NULL;
|
|
|
|
pfn = gpfnInitUserApi;
|
|
|
|
gcLoadUserApiHook = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hInstance = hModule;
|
|
|
|
ghmodUserApiHook = NULL;
|
|
|
|
gpfnInitUserApi = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection(&gcsUserApiHook);
|
|
|
|
|
|
|
|
if ( pfn )
|
|
|
|
{
|
|
|
|
pfn(uahShutdown, 0); // Shutdown.
|
|
|
|
|
|
|
|
RtlEnterCriticalSection(&gcsUserApiHook);
|
|
|
|
pfn1 = gpfnInitUserApi;
|
|
|
|
|
2011-09-19 12:43:44 +00:00
|
|
|
if ( --gcLoadUserApiHook == 0 )
|
2009-09-15 15:24:09 +00:00
|
|
|
{
|
|
|
|
if ( gcCallUserApiHook )
|
|
|
|
{
|
|
|
|
hInstance = NULL;
|
|
|
|
pfn1 = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hInstance = ghmodUserApiHook;
|
|
|
|
ghmodUserApiHook = NULL;
|
|
|
|
gpfnInitUserApi = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection(&gcsUserApiHook);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pfn1 ) pfn1(uahStop, 0);
|
|
|
|
|
|
|
|
return hInstance != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
InitUserApiHook(HINSTANCE hInstance, USERAPIHOOKPROC pfn)
|
|
|
|
{
|
|
|
|
USERAPIHOOK uah;
|
|
|
|
|
|
|
|
ResetUserApiHook(&uah);
|
|
|
|
|
|
|
|
RtlEnterCriticalSection(&gcsUserApiHook);
|
|
|
|
|
2011-09-18 13:12:44 +00:00
|
|
|
if (!pfn(uahLoadInit,&uah) || // Swap data, User32 to and Uxtheme from!
|
2017-02-20 17:31:50 +00:00
|
|
|
uah.ForceResetUserApiHook != ForceResetUserApiHook ||
|
2009-09-15 15:24:09 +00:00
|
|
|
uah.size <= 0 )
|
|
|
|
{
|
|
|
|
RtlLeaveCriticalSection(&gcsUserApiHook);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ghmodUserApiHook )
|
|
|
|
{
|
|
|
|
if ( ghmodUserApiHook != hInstance )
|
|
|
|
{
|
|
|
|
RtlLeaveCriticalSection(&gcsUserApiHook);
|
|
|
|
pfn(uahStop, 0);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gcLoadUserApiHook++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ghmodUserApiHook = hInstance;
|
|
|
|
// Do not over write GetRealWindowOwner.
|
|
|
|
RtlCopyMemory(&guah, &uah, sizeof(USERAPIHOOK) - sizeof(LONG));
|
|
|
|
gpfnInitUserApi = pfn;
|
|
|
|
gcLoadUserApiHook = 1;
|
|
|
|
gfUserApiHook = 1;
|
|
|
|
// Copy Message Masks
|
|
|
|
CopyMsgMask(&guah.DefWndProcArray,
|
|
|
|
&uah.DefWndProcArray,
|
|
|
|
&grgbDwpLiteHookMsg,
|
|
|
|
sizeof(grgbDwpLiteHookMsg));
|
|
|
|
|
|
|
|
CopyMsgMask(&guah.WndProcArray,
|
|
|
|
&uah.WndProcArray,
|
|
|
|
&grgbWndLiteHookMsg,
|
|
|
|
sizeof(grgbWndLiteHookMsg));
|
|
|
|
|
|
|
|
CopyMsgMask(&guah.DlgProcArray,
|
|
|
|
&uah.DlgProcArray,
|
|
|
|
&grgbDlgLiteHookMsg,
|
|
|
|
sizeof(grgbDlgLiteHookMsg));
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection(&gcsUserApiHook);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
RealMDIRedrawFrame(HWND hWnd, DWORD flags)
|
|
|
|
{
|
2011-08-25 08:51:26 +00:00
|
|
|
return NtUserxMDIRedrawFrame(hWnd);
|
2009-09-15 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
MDIRedrawFrame(HWND hWnd, DWORD flags)
|
|
|
|
{
|
|
|
|
BOOL Hook, Ret = FALSE;
|
|
|
|
|
2011-08-25 08:51:26 +00:00
|
|
|
LoadUserApiHook();
|
2009-09-15 15:24:09 +00:00
|
|
|
|
|
|
|
Hook = BeginIfHookedUserApiHook();
|
|
|
|
|
|
|
|
/* Bypass SEH and go direct. */
|
|
|
|
if (!Hook) return RealMDIRedrawFrame(hWnd, flags);
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
Ret = guah.MDIRedrawFrame(hWnd, flags);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
2018-01-15 10:36:45 +00:00
|
|
|
ERR("Got exception in hooked MDIRedrawFrame!\n");
|
2009-09-15 15:24:09 +00:00
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
EndUserApiHook();
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
USERAPIHOOK guah =
|
|
|
|
{
|
|
|
|
sizeof(USERAPIHOOK),
|
2013-11-17 22:10:42 +00:00
|
|
|
RealDefWindowProcA,
|
|
|
|
RealDefWindowProcW,
|
2009-09-15 15:24:09 +00:00
|
|
|
{NULL, 0},
|
2017-02-20 17:31:50 +00:00
|
|
|
RealGetScrollInfo,
|
|
|
|
RealSetScrollInfo,
|
|
|
|
NtUserEnableScrollBar,
|
|
|
|
RealAdjustWindowRectEx,
|
2013-11-17 22:10:42 +00:00
|
|
|
NtUserSetWindowRgn,
|
|
|
|
DefaultOWP,
|
|
|
|
DefaultOWP,
|
2009-09-15 15:24:09 +00:00
|
|
|
{NULL, 0},
|
2013-11-17 22:10:42 +00:00
|
|
|
DefaultOWP,
|
|
|
|
DefaultOWP,
|
2009-09-15 15:24:09 +00:00
|
|
|
{NULL, 0},
|
2017-02-20 17:31:50 +00:00
|
|
|
RealGetSystemMetrics,
|
|
|
|
RealSystemParametersInfoA,
|
|
|
|
RealSystemParametersInfoW,
|
|
|
|
ForceResetUserApiHook,
|
|
|
|
RealDrawFrameControl,
|
|
|
|
NtUserDrawCaption,
|
|
|
|
RealMDIRedrawFrame,
|
|
|
|
GetRealWindowOwner,
|
2009-09-15 15:24:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL WINAPI RegisterUserApiHook(PUSERAPIHOOKINFO puah)
|
|
|
|
{
|
|
|
|
UNICODE_STRING m_dllname1;
|
|
|
|
UNICODE_STRING m_funname1;
|
|
|
|
|
|
|
|
if (puah->m_size == sizeof(USERAPIHOOKINFO))
|
|
|
|
{
|
2022-05-02 12:26:10 +00:00
|
|
|
WARN("RegisterUserApiHook: %S and %S\n", puah->m_dllname1, puah->m_funname1);
|
2009-09-15 15:24:09 +00:00
|
|
|
RtlInitUnicodeString(&m_dllname1, puah->m_dllname1);
|
|
|
|
RtlInitUnicodeString(&m_funname1, puah->m_funname1);
|
|
|
|
return NtUserRegisterUserApiHook( &m_dllname1, &m_funname1, 0, 0);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL WINAPI UnregisterUserApiHook(VOID)
|
|
|
|
{
|
2011-09-19 12:43:44 +00:00
|
|
|
return NtUserUnregisterUserApiHook();
|
2009-09-15 15:24:09 +00:00
|
|
|
}
|