how to use the HOOK

  • Thread starter msnews.micorsoft.com
  • Start date
M

msnews.micorsoft.com

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.
 
G

Guest

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 ).
 
W

William DePalo [MVP VC++]

msnews.micorsoft.com said:
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.

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
 
W

William DePalo [MVP VC++]

Nadav said:
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/defaul...port/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
 
A

Abhinaba Basu

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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top