473,804 Members | 3,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,wPa ram,lParam);

if(nCode == HCBT_ACTIVATE)
{
counter++;
::PostMessage(h parentWin,USER_ MESSAGE_1,wPara m,lParam);//send a user
message to my application's main window
}
return CallNextHookEx( hHook,nCode,wPa ram,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 2200
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.micorso ft.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,wPa ram,lParam);

if(nCode == HCBT_ACTIVATE)
{
counter++;
::PostMessage(h parentWin,USER_ MESSAGE_1,wPara m,lParam);//send a user
message to my application's main window
}
return CallNextHookEx( hHook,nCode,wPa ram,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.micorso ft.com" <fq******@163.c om> wrote in message
news:en******** *****@TK2MSFTNG P11.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,wPa ram,lParam);

if(nCode == HCBT_ACTIVATE)
{
counter++;
::PostMessage(h parentWin,USER_ MESSAGE_1,wPara m,lParam);//send a user
message to my application's main window
}
return CallNextHookEx( hHook,nCode,wPa ram,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***@discussi ons.microsoft.c om> wrote in message
news:1E******** *************** ***********@mic rosoft.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(".SHAR ED_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
SetWindowsHookE x().

"msnews.micorso ft.com" <fq******@163.c om> wrote in message
news:en******** *****@TK2MSFTNG P11.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,wPa ram,lParam);

if(nCode == HCBT_ACTIVATE)
{
counter++;
::PostMessage(h parentWin,USER_ MESSAGE_1,wPara m,lParam);//send a user
message to my application's main window
}
return CallNextHookEx( hHook,nCode,wPa ram,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
4759
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. I modified the class keeping a majority of his code, and built a c#(.net) front end to use this c++ built core library, passing it the hook type of EM_SETTEXTEX (WM_USER + 97) and the threadID of the richedit box, his class comes up telling me...
5
7794
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 generate an error (1404: Invalid hook handle.). A part of code is this: Install Hook: .... CBTCallBack = new GlobalHook.GlobalHook.HookProc(CBTCallBackEventHandler); CBTHandle = GlobalHook.SetWindowsHookEx(GlobalHook.HookType.WH_CBT,CBTCallBack,
1
2655
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 msg(combination of wm_settext and em_replacesel). Below are a few lines of code of what im trying to accomplish: private const uint WM_USER = 0x0400; EM_SETTEXTEX = (WM_USER + 97) // Comes from an enum I have. (hooktype) I have gotten the ThreadID...
0
1871
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 created named LocalWindowsHook that imports the below API calls protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func,
0
1703
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. I modified the class keeping a majority of his code, and built a c#(.net) front end to use this c++ built core library, passing it the hook type of EM_SETTEXTEX (WM_USER + 97) and the threadID of the richedit box, his class comes up telling me...
7
26552
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 nCode, int wParam, int lParam) {
2
4712
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 paint message and injects the custom UI. From the reading I've done, it is possible to install a system hook with an unmanaged stub that passes execution off to a managed C# delegate (http://www.codeproject.com/csharp/GlobalSystemHook.asp), but...
0
2496
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 containing a Edit(textbox) and button to test at first. When I change text in Edit or press button to setText value, I want to monitor the change event. But the event code always return 0, why?
3
2168
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: ----------------------------------------------------------------------- import sys
22
6441
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
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10087
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.