Techwiki:RegisterUserApiHook

From ReactOS Wiki
(Redirected from RegisterUserApiHook)
Jump to: navigation, search

RegisterUserApiHook is a function that is called once per session and makes user32 load the specified module for each process and call the specified callback function. It is implemented in user32 to be used exclusively by uxtheme as it abstracts the functionality needed to apply themes in all windows.

This is the prototype in Windows XP:

BOOL WINAPI RegisterUserApiHook(HINSTANCE hInstance, FARPROC CallbackFunc);

And this is the prototype in Windows 2003:

typedef struct _USERAPIHOOKINFO
{
 DWORD m_size;
 LPCWSTR m_dllname1;
 LPCWSTR m_funname1;
 LPCWSTR m_dllname2;
 LPCWSTR m_funname2;
}USERAPIHOOKINFO,*PUSERAPIHOOKINFO;

BOOL WINAPI RegisterUserApiHook(PUSERAPIHOOKINFO ApiHookInfo);

In both cases we give to RegisterUserApiHook the dll that is going to be loaded and the callback function.


This is the prototype of the callback function:

typedef LRESULT(CALLBACK *WNDPROC_OWP)(HWND,UINT,WPARAM,LPARAM,ULONG_PTR,PDWORD);

typedef struct _UAHOWP
{
  BYTE*  MsgBitArray;
  DWORD  Size;
} UAHOWP, *PUAHOWP;

typedef struct tagUSERAPIHOOK
{
  DWORD   size;
  WNDPROC DefWindowProcA;
  WNDPROC DefWindowProcW;
  UAHOWP  DefWndProcArray;
  FARPROC GetScrollInfo;
  FARPROC SetScrollInfo;
  FARPROC EnableScrollBar;
  FARPROC AdjustWindowRectEx;
  FARPROC SetWindowRgn;
  WNDPROC_OWP PreWndProc;
  WNDPROC_OWP PostWndProc;
  UAHOWP  WndProcArray;
  WNDPROC_OWP PreDefDlgProc;
  WNDPROC_OWP PostDefDlgProc;
  UAHOWP  DlgProcArray;
  FARPROC GetSystemMetrics;
  FARPROC SystemParametersInfoA;
  FARPROC SystemParametersInfoW;
  FARPROC ForceResetUserApiHook;
  FARPROC DrawFrameControl;
  FARPROC DrawCaption;
  FARPROC MDIRedrawFrame;
  FARPROC GetRealWindowOwner;
} USERAPIHOOK, *PUSERAPIHOOK;

typedef enum _UAPIHK
{
  uahLoadInit,
  uahStop,
  uahShutdown
} UAPIHK, *PUAPIHK;


DWORD CALLBACK 
UserApiHookProc(UAPIHK State, PUSERAPIHOOK Info);


User32 gives to the callback function an USERAPIHOOK struct filled with the original implementation of the functions that the library can use from user32. Then the callback replaces the functions from user32 with its own functions. So for example if a program calls DefWindowProc, user32 instead of calling its implementation it calls the function specified by the UserApiHook callback.


If an application has installed the api hook, any call to RegisterUserApiHook will fail. Api hook is uninstalled if the process that called RegisterUserApiHook calls UnregisterUserApiHook, or if it is terminated.


In Windows XP the callback function is internal in uxtheme.dll and isn’t exported. However in Windows 2003 this function must be exported because RegisterUserApiHook must be given its name and not a pointer to it. It is exported from uxtheme.dll with the name ThemeInitApiHook.


References

http://www.winehq.org/pipermail/wine-devel/2005-March/035286.html

http://www.winehq.org/pipermail/wine-devel/2005-March/035245.html

http://www.arabteam2000-forum.com/lofiversion/index.php/t139449.html