Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use the HOOK

0 views
Skip to first unread message

msnews.micorsoft.com

unread,
Jul 12, 2004, 1:30:31 AM7/12/04
to
i want to receive a message when a window was activated.
I use the WH_CBT type hook to process,the code is following:
LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam, LPARAM lParam)
{
HWND hWnd;
if(nCode < 0)
return CallNextHookEk(hHook,nCode,wParam,lParam);

if(nCode == HCBT_ACTIVATE)
{
counter++;
::PostMessage(hparentWin,USER_MESSAGE_1,wParam,lParam);//send a user
message to my application's main window
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}

But the code can not run rightly.
who can help me, it would be better to supply the code.


Nadav

unread,
Jul 12, 2004, 4:26:02 AM7/12/04
to
I would suggest you NOT to use the CBT Hook but to replace the windows WindowProc with you custom procedure, after receiving the messages at your custom procedure you should route your message to the original procedure, this will enable you to get ANY message being send to a specific window, BUT require you to have the windows handle before applying the custom procedure ( this can be achieved by the FindWindow/EnumWindows API ), to replace the original window proc wuth your custom proc use SetWindoLong/Ptr, the returned valu is a pointer to the original WNDPROC, keep it so you could route messages back to original WNDPROC... ( this process is also known as window sub-classing ).
--
Nadav
http://www.ddevel.com

William DePalo [MVP VC++]

unread,
Jul 12, 2004, 3:00:20 PM7/12/04
to
"msnews.micorsoft.com" <fqyd...@163.com> wrote in message
news:envgaG9...@TK2MSFTNGP11.phx.gbl...

How does it fail?

Let me guess: Do you have the hook handle in a shared segment in the DLL? If
you don't when the hook procedure runs in any application but the one that
planted it, it will see a null window handle.

Regards,
Will


William DePalo [MVP VC++]

unread,
Jul 12, 2004, 3:08:19 PM7/12/04
to
"Nadav" <Na...@discussions.microsoft.com> wrote in message
news:1EE455FF-FDCA-4583...@microsoft.com...

> I would suggest you NOT to use the CBT Hook but to replace
> the windows WindowProc with you custom procedure, after
> receiving the messages at your custom procedure you should
> route your message to the original procedure, this will enable
> you to get ANY message being send to a specific window,
> BUT require you to have the windows handle before applying
> the custom procedure ( this can be achieved by the
> FindWindow/EnumWindows API ), to replace the original
> window proc wuth your custom proc use SetWindoLong/Ptr,
> the returned valu is a pointer to the original WNDPROC,
> keep it so you could route messages back to original
> WNDPROC... ( this process is also known as window sub-classing ).

Yes, but in Win32 processes each run in their own address apaces. So if the
subclasser and subclassee are in different process then special steps need
to be taken well before you can think of call SetWindowLong:

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q125/6/80.asp&NoWebContent=1

And as one of the suggested methods involves using a hook, the OP's idea
about using a CBT hook to catch window activations is the canonical approach
in the _general_ case.

Regards,
Will


Abhinaba Basu

unread,
Jul 13, 2004, 5:39:53 AM7/13/04
to
To use this method. Most crucial is to find out how you are storing the
hparentWin. If that is just a variable then it will not work. For every
application that loads a dll, the data sections are created afresh. So each
application will get a new copy of the variables. So in the application you
have hooked to, the value of the handle will be different and your
PostMessage will go to a undefined window. So place the handle as follows in
the source file as

#pragma data_seg(".SHARED_SECT")
HWND hparentWin = NULL;
#pragma data_seg()

Create and add a .def file to your project and add the following lines to it

SECTIONS
.SHARED_SECT Read Write Shared

This makes the section .SHARED_SECT shared and hence all the variables in
this section (hparentWin) will be shared across all aplications that load
the dll. So from your application just set the hparentWin to the handle of
your window and you should be getting the messages, if you correctly called
SetWindowsHookEx().

"msnews.micorsoft.com" <fqyd...@163.com> wrote in message
news:envgaG9...@TK2MSFTNGP11.phx.gbl...

0 new messages