473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to use the HOOK

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,lPa ram);//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.
Nov 17 '05 #1
4 2173
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
"msnews.micorsoft.com" wrote:
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,lPa ram);//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.

Nov 17 '05 #2
"msnews.micorsoft.com" <fq******@163.com> wrote in message
news:en*************@TK2MSFTNGP11.phx.gbl...
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,lPa ram);//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
Nov 17 '05 #3
"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.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...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
Nov 17 '05 #4
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" <fq******@163.com> wrote in message
news:en*************@TK2MSFTNGP11.phx.gbl...
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,lPa ram);//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.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: murlbrown | last post by:
I am using a class library built by michael kennedy that creates a system wide hook on the WH_KEYBOARD_LL, but I wanted to changed it to become a hook for a RichEdit box in another application. ...
5
by: Arnold | last post by:
Hi, I want to make a Global CBT Hook, but the CallBack Method is never raised. SetWindowsHookEx return me a Handle to the Hook, but when i try to uninstall the hook the UnhookWindowsHookEx...
1
by: murlbrown | last post by:
Basicly im trying to determine when a new line has been appended to the textbox of another application. Using spy++ I see that the msg (wm_user + 97) is being sent, which is the EM_SETTEXTEX...
0
by: gekoscan | last post by:
Hello, http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/ I don't know if any of you are familiar with Dino Esposito article in MSDN magazine on .NET Hooks but I used the class he...
0
by: murlbrown | last post by:
I am using a class library built by michael kennedy that creates a system wide hook on the WH_KEYBOARD_LL, but I wanted to changed it to become a hook for a RichEdit box in another application. ...
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
2
by: Lunchtimemama | last post by:
I am trying to modify another application's window (Google Talk) to insert some UI of my own. I think the best method would be to install a remote local hook in gtalk's thread that intercepts the...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
3
by: Sami | last post by:
Hello, I am new to Python. I tried to hook my own ExceptionPrintingFunction sys.excepthook but it does not work. This is what I wrote:...
22
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.