473,856 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eventcode all 0, Hook Edit change

I download the demo
http://msdn.microsoft.com/msdnmag/is...0/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?
############### ############### #########Window sHook.cs:

using System;
using System.Runtime. InteropServices ;

namespace MsdnMag
{
#region Class HookEventArgs
public class HookEventArgs : EventArgs
{
public int HookCode; // Hook code
public IntPtr wParam; // WPARAM argument
public IntPtr lParam; // LPARAM argument
}
#endregion

#region Enum HookType
// Hook Types
public enum HookType : int
{
WH_JOURNALRECOR D = 0,
WH_JOURNALPLAYB ACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDID LE = 11,
WH_CALLWNDPROCR ET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
#endregion

#region Class LocalWindowsHoo k
public class LocalWindowsHoo k
{
//
*************** *************** *************** *************** ************
// Filter function delegate
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Internal properties
protected IntPtr m_hhook = IntPtr.Zero;
protected HookProc m_filterFunc = null;
protected HookType m_hookType;
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Event delegate
public delegate void HookEventHandle r(object sender, HookEventArgs
e);
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Event: HookInvoked
public event HookEventHandle r HookInvoked;
protected void OnHookInvoked(H ookEventArgs e)
{
if (HookInvoked != null)
HookInvoked(thi s, e);
}
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Class constructor(s)
public LocalWindowsHoo k(HookType hook)
{
m_hookType = hook;
m_filterFunc = new HookProc(this.C oreHookProc);
}
public LocalWindowsHoo k(HookType hook, HookProc func)
{
m_hookType = hook;
m_filterFunc = func;
}
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Default filter function
protected int CoreHookProc(in t code, IntPtr wParam, IntPtr lParam)
{
Console.Write(" coreHookPro=");
Console.WriteLi ne(code);

if (code < 0)
return CallNextHookEx( m_hhook, code, wParam, lParam);

// Let clients determine what to do
HookEventArgs e = new HookEventArgs() ;
e.HookCode = code;
e.wParam = wParam;
e.lParam = lParam;
OnHookInvoked(e );

// Yield to the next hook in the chain
return CallNextHookEx( m_hhook, code, wParam, lParam);
}
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Install the hook
public void Install()
{
m_hhook = SetWindowsHookE x(
m_hookType,
m_filterFunc,
IntPtr.Zero,
(int) AppDomain.GetCu rrentThreadId() );
}
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Uninstall the hook
public void Uninstall()
{
UnhookWindowsHo okEx(m_hhook);
}
//
*************** *************** *************** *************** ************
#region Win32 Imports
//
*************** *************** *************** *************** ************
// Win32: SetWindowsHookE x()
[DllImport("user 32.dll")]
protected static extern IntPtr SetWindowsHookE x(HookType code,
HookProc func,
IntPtr hInstance,
int threadID);
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Win32: UnhookWindowsHo okEx()
[DllImport("user 32.dll")]
protected static extern int UnhookWindowsHo okEx(IntPtr hhook);
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Win32: CallNextHookEx( )
[DllImport("user 32.dll")]
protected static extern int CallNextHookEx( IntPtr hhook,
int code, IntPtr wParam, IntPtr lParam);
//
*************** *************** *************** *************** ************
#endregion
}
#endregion
}

############### ############### ############### EditChange.cs

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;

namespace MsdnMag
{
#region Enum EditHookAction
// CBT hook actions
public enum EditHookAction : int
{
EN_CHANGE = 0x300, //编辑框*的 文本己更新
EN_ERRSPACE=0x0 500, //编辑框内* 不足
EN_HSCROLL=0x06 01, //用户点击了 水平滚动条
EN_KILLFOCUS =0x0200,//编辑框*在 失去输入焦 点
EN_MAXTEXT=0x05 01, //插入的内容 被截*
EN_SETFOCUS=0x0 100, //编辑框获得 输入焦点
EN_UPDATE=0x040 0, //编辑框*的 文本将要更 新
EN_VSCROLL=0x06 02 //用户点击了 垂直滚动条 消息含义

//EMN_CHANGE=0x00 000001

}
#endregion

public class EditEventArgs : EventArgs
{
public IntPtr Handle; // Win32 handle of the window
public string Title; // caption of the window
public string ClassName; // class of the window
public string textValue;

}

#region Class EditChange
public class EditChange : LocalWindowsHoo k
{
//
*************** *************** *************** *************** ************
// Event delegate
public delegate void EditEventHandle r(object sender,
EditEventArgs e);
//
*************** *************** *************** *************** ************
const int WM_GETTEXT = 0x000D;
StringBuilder Buff = new StringBuilder(9 0240);

//
*************** *************** *************** *************** ************
// Events
public event EditEventHandle r TextEditChange;
//public event EditEventHandle r EditSetFocus;

//
*************** *************** *************** *************** ************
//
*************** *************** *************** *************** ************
// Internal properties
protected IntPtr m_hwnd = IntPtr.Zero;
protected string m_title = "";
protected string m_class = "";
protected string textValue = "";

//
*************** *************** *************** *************** ************
//
*************** *************** *************** *************** ************
// Class constructor(s)
public EditChange() : base(HookType.W H_CALLWNDPROC)
{
this.HookInvoke d += new HookEventHandle r(EditHookInvok ed);
}
public EditChange(Hook Proc func)
: base(HookType.W H_CALLWNDPROC, func)
{
this.HookInvoke d += new HookEventHandle r(EditHookInvok ed);
}
//
*************** *************** *************** *************** ************
//
*************** *************** *************** *************** ************
// Handles the hook event
private void EditHookInvoked (object sender, HookEventArgs e)
{
EditHookAction code = (EditHookAction )e.HookCode;
Console.WriteLi ne(sender);
Console.Write(" Event hook code=");
Console.WriteLi ne(code);
Console.WriteLi ne(e.ToString() );
Console.Write(" wparam=");
Console.WriteLi ne(e.wParam);
Console.Write(" lparam=");
Console.WriteLi ne(e.lParam);
IntPtr wParam = e.wParam;
IntPtr lParam = e.lParam;

// Handle hook events (only a few of available actions)
switch (code)
{
case EditHookAction. EN_CHANGE:
Console.Write(" EN_CHANGE");
HandleEditChang eEvent(wParam, lParam);
break;
}

return;
}
//
*************** *************** *************** *************** ************
//
*************** *************** *************** *************** ************
// Handle the CREATEWND hook event
private void HandleEditChang eEvent(IntPtr wParam, IntPtr
lParam)
{
// Cache some information
UpdateWindowDat a(wParam,lParam );

// raise event
OnTextEditChang e();
}
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Read and store some information about the window
private void UpdateWindowDat a(IntPtr wParam, IntPtr lParam)
{
Console.Write(" WPARAM=");
Console.WriteLi ne(wParam);
// Cache the window handle
m_hwnd = lParam;

// Cache the window's class name
StringBuilder sb1 = new StringBuilder() ;
sb1.Capacity = 40;
GetClassName(m_ hwnd, sb1, 40);
m_class = sb1.ToString();

// Cache the window's title bar
StringBuilder sb2 = new StringBuilder() ;
sb2.Capacity = 256;
GetWindowText(m _hwnd, sb2, 256);
m_title = sb2.ToString();

// get Text value
SendMessage(m_h wnd, WM_GETTEXT, Buff.Capacity, Buff);
textValue = Buff.ToString() ;
Console.Write(" Text=");
Console.Write(B uff.ToString()) ;
}
//
*************** *************** *************** *************** ************
//
*************** *************** *************** *************** ************
// Helper functions that fire events by executing user code
protected virtual void OnTextEditChang e()
{
if (TextEditChange != null)
{
EditEventArgs e = new EditEventArgs() ;
PrepareEventDat a(e);
TextEditChange( this, e);
}
}
//
*************** *************** *************** *************** ************
//
*************** *************** *************** *************** ************
// Prepare the event data structure
private void PrepareEventDat a(EditEventArgs e)
{
e.Handle = m_hwnd;
e.Title = m_title;
e.ClassName = m_class;
e.textValue = textValue;
}
//
*************** *************** *************** *************** ************

#region Win32 Imports
//
*************** *************** *************** *************** ************
// Win32: GetClassName
[DllImport("user 32.dll")]
protected static extern int GetClassName(In tPtr hwnd,
StringBuilder lpClassName, int nMaxCount);
//
*************** *************** *************** *************** ************

//
*************** *************** *************** *************** ************
// Win32: GetWindowText
[DllImport("user 32.dll")]
protected static extern int GetWindowText(I ntPtr hwnd,
StringBuilder lpString, int nMaxCount);
//
*************** *************** *************** *************** ************
[DllImport("user 32")]
private static extern int SendMessage(Int Ptr hwnd, int Msg, int
wParam, StringBuilder lParam);
#endregion
}
#endregion
}
############### ############### ############### HookForm.cs

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using MsdnMag;

namespace TestCbtHook_CS
{
public partial class HookForm : Form
{
private EditChange cbt;
public HookForm()
{
InitializeCompo nent();
cbt = new EditChange();
cbt.TextEditCha nge += new
EditChange.Edit EventHandler(Ed itTextChange);
cbt.Install();
//MessageBox.Show ("Hello, world!", "Courtesy of Cutting
Edge");
}
//
*************** *************** *************** *************** *************** ***
// Prepare the hook object to work
//
*************** *************** *************** *************** *************** ***
private void button1_Click(o bject sender, EventArgs e)
{

textBox1.Text = "new text";
}
override
protected void OnFormClosing(F ormClosingEvent Args e)
{
cbt.Uninstall() ;
base.OnFormClos ing(e);
Console.WriteLi ne("onFormClosi ng unstall");
}
public void EditTextChange( object sender, EditEventArgs e)
{
Console.WriteLi ne("HookForm EditeTextChange ");
Console.WriteLi ne(sender.ToStr ing());
Console.WriteLi ne(e.ClassName) ;
}

[STAThread]
static void Main()
{
Application.Run (new HookForm());
}
}
}

using System;
namespace TestCbtHook_CS
{
partial class HookForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
Console.WriteLi ne("Disponse") ;

if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(98, 130);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(75, 23);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(98, 76);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(100, 25);
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "1";
//
// HookForm
//
this.AutoScaleD imensions = new System.Drawing. SizeF(8F,
15F);
this.AutoScaleM ode =
System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 258);
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.button1 );
this.Name = "HookForm";
this.Text = "HookForm";
this.ResumeLayo ut(false);
this.PerformLay out();

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.TextBox textBox1;
}
}

Jun 4 '06 #1
0 2497

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

Similar topics

1
3422
by: ayiiq180 | last post by:
my hook already in a dll and the handle is shared,but the hook cant work well,when i run the application,My mouse click the application's view,the hook work well,but when i click the other place(like taskbar),it cant work,why?help me. this is the hook: // HookDll.cpp : Defines the initialization routines for the DLL. // #include "stdafx.h" #include "HookDll.h"
5
7102
by: Logan Mckinley | last post by:
I need to know where the cursor is (x,y) and when it moves even when it is not over my form. I know i can get the current location with: System.Windows.Forms.Cursor.Position.X System.Windows.Forms.Cursor.Position.X But that does not raise an event and using a timer would be to processor intensive. In MFC you would write a mousehook to solve this problem but when i run the C# MouseHook code for from MSDN...
1
1884
by: | last post by:
Give an example:When user pressed "B" or "b",HOOK get that message and let TextBox1.text display "a". The executing founction is as following: **************************************** IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam) { if( code < 0 ) return CallNextHookEx(_nextHookPtr,code, wparam, lparam); if( wparam.ToInt32() == 98 || wparam.ToInt32() == 66 ) {
3
8432
by: june | last post by:
Hi, I am coding for an application with dialog window. I need intercept mouse input. I need catch raw input, pretty much everything for WM_INPUT: such as Left/Middle/Right button down/up, srcoll/wheel, pull and drag, device source. I checked SetWindowsHookEx, it seems it can't catch so much infor, only LButton RButton Down/Up? What function should I use or maybe I should ask how to hook Dialog box message Proc? Thanks
1
7687
by: blueturtle | last post by:
Hi, I've implemented a hook (WH_CALLWNDPRO), using the sample posted at MSDN magazine. ( see url: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/ ) I succeed in intercepting the relevant messages, however if I try to change the parameter to the original message, it doesn't work. It seems like I'm doing something wrong with the Interop services.
7
26559
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) {
6
2532
by: ewolfman | last post by:
Hi, Is there any way in which I can monitor / hook the Registry, and upon a call to a specific key from a specific application - swap the returned value? I was thinking of using this method to overcome the "click" sound of the WebBrowser, without having to affect the Registry permanently. This way I could monitor the call for the sound file to be played (located in
22
6451
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
11065
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
10700
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...
1
10795
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9536
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7934
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
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5763
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...
1
4581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.