Problem with SetWindowsHookEx 
Author Message
 Problem with SetWindowsHookEx

Hello I have a problem with setting a system hook for all messages using the
following call:

    SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, hInst, 0);

CallWndProc is declared in the same code file above as follows:

    LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam);

Everything works fine if I do it for just a single thread but as soon as I
try a system hook it causes all kinds of memory errors and all windows close
and the shell also closes. I fix it with logging out. I have read somewhere
that the callback function for system hooks should be in a library but I am
not sure what that means since I am new to C++. Using VC++ 6.0 SP3. Any help
would be greatly appreciated!!
Thanks,
KEn



Thu, 27 Jun 2002 03:00:00 GMT  
 Problem with SetWindowsHookEx


Quote:
> Hello I have a problem with setting a system hook for all messages using
the
> following call:

>     SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, hInst, 0);

> CallWndProc is declared in the same code file above as follows:

>     LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam);

> Everything works fine if I do it for just a single thread but as soon as I
> try a system hook it causes all kinds of memory errors and all windows
close
> and the shell also closes. I fix it with logging out. I have read
somewhere
> that the callback function for system hooks should be in a library but I
am
> not sure what that means since I am new to C++. Using VC++ 6.0 SP3. Any
help
> would be greatly appreciated!!

Library, yes. DLL = Dynamically Linked Library.

What are you trying to do. A system-wide hook such as this is going to get
called everytime any application calls SendMessage(). The DLL that contains
your hook potentially will be injected into the address space of every
application. Make a mistake and you can crash apps you didn't write and
generally raise the level of entropy on the system.

Regards,
Will



Thu, 27 Jun 2002 03:00:00 GMT  
 Problem with SetWindowsHookEx


Quote:
> Thanks for the info William,

You are welcome.

Quote:
> I am trying to monitor when certain windows are destroyed.

In that case, I'd use a computer based training hook and look for
HCBT_DESTROYWND notifications. Check the docs for CBTProc.

Quote:
>I thought the system hook would be the best but
> since it may be more hassle than it is worth I may just set the hook on
the
> threads that the user wants and keep track of what window and the hook
> handle. Is there any problem to have them point to the same callback
> procedure as long as I keep track of which hook handle had to be used when
I
> call CallNextHookEx?

Yeah. Note this caveat in the help entry for SetWindowsHookEx():

"An error may occur if the hMod parameter is NULL and the dwThreadId
parameter is zero or specifies the identifier of a thread created by another
process. "

The problem is your hook code has to reside in the address space of the
programs whose events are being tarppped. That's no problem if the thread
you choose is one of the caller's threads because all threads in a process
share the same address space. If however the thread is outside of the caller
then the o/s has to map the hook code into the thread's address space before
it calls the hook procedure. The operating system performs that magic only
when the hook is DLL based.

In short, you'll have the same hassle unless you limit yourself to hooking
your own app.

Regards,
Will



Fri, 28 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problems with SetWindowsHookEx

2. Problem involving SetWindowsHookEx

3. problem with SetWindowsHookEx

4. Problem with SetWindowsHookEx

5. SetWindowsHookEx - KeyboardProc Problem

6. SetWindowsHookEx(), what's my problem?

7. SetWindowsHookEx() problem with NT 4.0

8. Head scratching problem on Keyboard hook (SetWindowsHookEx)

9. SetWindowsHookEx

10. SetWindowsHookEx lost when switching appl

11. Help using SetWindowsHookEx

12. SetWindowsHookEx and ATL service

 

 
Powered by phpBB® Forum Software