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 - 12 Jul 2004 09:26 GMT
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 ).

Signature
Nadav
http://www.ddevel.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:
[quoted text clipped - 15 lines]
> But the code can not run rightly.
> who can help me, it would be better to supply the code.
William DePalo [MVP VC++] - 12 Jul 2004 20:08 GMT
> I would suggest you NOT to use the CBT Hook but to replace
> the windows WindowProc with you custom procedure, after
[quoted text clipped - 8 lines]
> 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/s
upport/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
William DePalo [MVP VC++] - 12 Jul 2004 20:00 GMT
> i want to receive a message when a window was activated.
> I use the WH_CBT type hook to process,the code is following:
[quoted text clipped - 15 lines]
> 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
Abhinaba Basu - 13 Jul 2004 10:39 GMT
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().
> i want to receive a message when a window was activated.
> I use the WH_CBT type hook to process,the code is following:
[quoted text clipped - 15 lines]
> But the code can not run rightly.
> who can help me, it would be better to supply the code.