tags:

views:

190

answers:

3

I'm trying to create a Windows hook, specifically to catch HSHELL_WINDOWCREATED messages.
However, my hook proc never gets called.

My dll has a function to install the hook:

hHook = SetWindowsHookEx(WH_SHELL, (HOOKPROC)CreateWindowHook, hinst, 0);

hinst was filled in by DllMain(), at the DLL_PROCESS_ATTACH message.
CreateWindowHook is defined as:

LRESULT CALLBACK CreateWindowHook(int code, WPARAM wParam, LPARAM lParam) {
    if (code == HSHELL_WINDOWCREATED) {
        // snip 
    }
    return CallNextHookEx(0, code, wParam, lParam); 
}

I expect the proc to be called whenever a new top level window is created, but it is never called. What am I missing? Thanks.

More information:
According to this article -- http://msdn.microsoft.com/en-us/library/ms644959(VS.85).aspx -- the app that wants to receive WH_SHELL messages has to register itself via SystemParametersInfo():

MINIMIZEDMETRICS st;
st.cbSize = sizeof(MINIMIZEDMETRICS);
st.iArrange = ARW_HIDE;
BOOL ret;

ret = SystemParametersInfo(
  SPI_SETMINIMIZEDMETRICS,
  sizeof(MINIMIZEDMETRICS),
  &st,
  0);

Is that call made in the dll, or the app that loads the dll containing the hook, or doesn't it matter?

A: 

Are you running on Vista? If so you are likely running into a UAC issue. By specifying 0 as the last parameter you are asking for a global hook. This can only do done as an administrator.

Try re-running as an administrator and see if that fixes your problem.

JaredPar
Thanks for the reply. WinXP, my account is an admin account.
Number8
Actually, a global hook can be set as a non-admin, but only non-admin processes will be hooked.
Andy
A: 

Is your hhook both initialised and in a shared section ? see here for details.

Bob Moore
+2  A: 

I answered a similar question to this a while back, does any of this help? How to add a system “windows hook” so as to be notified of windows being created/activated?

Andy