473,898 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hooking problem...

TMP
Below is the code snippet that I'm having problems with, the hook seems to
work (hookResult returns a value other than zero) but then explorer
crashes.Also I'm aware of the fact that instead of specifying a dll with my
hook function embedded, I used my executable's module HINSTANCE. But I guess
no problem there since it works with different hook types.So can anyone
explain why explorer keeps crashing? (on Windows XP Professional) Thanks in
advance.

....

{
IntPtr taskBarHandle = IntPtr.Zero;

taskBarHandle = API.FindWindow( "Shell_TrayWnd" , null);

API.HOOKPROC msgHookProc = new TMP.WinXP.API.H OOKPROC(MsgHook Proc);

GCHandle gcHandle = GCHandle.Alloc( msgHookProc, GCHandleType.No rmal);
uint threadId = 0;

uint processId = 0;

threadId = API.GetWindowTh readProcessId(t askBarHandle, out processId);

if(threadId != 0)

hookResult = API.SetWindowsH ookEx(API.WH_GE TMESSAGE,
msgHookProc,Mar shal.GetHINSTAN CE((typeof(Form 1).Module)), threadId);

Console.WriteLi ne("Hook Result = " + hookResult.ToSt ring());

}

....

private IntPtr MsgHookProc(int nCode, IntPtr wParam, IntPtr lParam)

{

if(nCode < 0 )

return API.CallNextHoo kEx(hookResult, nCode, wParam, lParam);
API.MSG message = (API.MSG) Marshal.PtrToSt ructure(lParam, typeof(API.MSG) );

MessageBox.Show (message.pt.x.T oString());
return API.CallNextHoo kEx(hookResult, nCode, wParam, lParam);

}


Nov 15 '05 #1
1 2408
Hi TMP,

You are trying to install a hook to a thread which is not created by your
process. In this case your hook procedure has to be in a DLL because what
windows does is to inject that dll to the target process (like a code
executed in the target thread calls LoadLibrary). The problem is that
unmanaged code cannot load and use managed DLL that's why such a hooks are
not possible using .NET. With managed code you can only hook your own
process.

--
HTH
B\rgds
100 [C# MVP]

"TMP" <de*****@nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Below is the code snippet that I'm having problems with, the hook seems to
work (hookResult returns a value other than zero) but then explorer
crashes.Also I'm aware of the fact that instead of specifying a dll with my hook function embedded, I used my executable's module HINSTANCE. But I guess no problem there since it works with different hook types.So can anyone
explain why explorer keeps crashing? (on Windows XP Professional) Thanks in advance.

...

{
IntPtr taskBarHandle = IntPtr.Zero;

taskBarHandle = API.FindWindow( "Shell_TrayWnd" , null);

API.HOOKPROC msgHookProc = new TMP.WinXP.API.H OOKPROC(MsgHook Proc);

GCHandle gcHandle = GCHandle.Alloc( msgHookProc, GCHandleType.No rmal);
uint threadId = 0;

uint processId = 0;

threadId = API.GetWindowTh readProcessId(t askBarHandle, out processId);

if(threadId != 0)

hookResult = API.SetWindowsH ookEx(API.WH_GE TMESSAGE,
msgHookProc,Mar shal.GetHINSTAN CE((typeof(Form 1).Module)), threadId);

Console.WriteLi ne("Hook Result = " + hookResult.ToSt ring());

}

...

private IntPtr MsgHookProc(int nCode, IntPtr wParam, IntPtr lParam)

{

if(nCode < 0 )

return API.CallNextHoo kEx(hookResult, nCode, wParam, lParam);
API.MSG message = (API.MSG) Marshal.PtrToSt ructure(lParam, typeof(API.MSG) );
MessageBox.Show (message.pt.x.T oString());
return API.CallNextHoo kEx(hookResult, nCode, wParam, lParam);

}

Nov 15 '05 #2

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

Similar topics

1
1283
by: Raja | last post by:
Dears, I apologize in advance if this is the wrong place to post, sorry for the inconvenience. Is there any book or web site or any kind of resource is available for Windows hooking and subcalssing in C# / VB.Net (in general .Net)? OR, is there any link in Microsoft sites / MSDN ?
0
1181
by: Jim Hubbard | last post by:
Is GDI Hooking possible in VB.Net?
3
1700
by: Seth Gecko | last post by:
Hi I want to make an application (in C#) that can catch text written to a Richedit control in another application. Using Spy++ I can see that the message I want to intercept is called EM_REPLACESEL. I have been googling and I am pretty sure that the way forward is using SetWindowsHookEx, but the hook I should use, I am not so sure about. WM_GETTEXT seems the only real option. Another problem is that SetWindowsHookEx used from .Net will...
1
5373
by: Matthew Kelly | last post by:
I have pulled together a VB.net project that hooks the keyboard (Ref. Paul Kimmel's hooking program) and allow the user to send "mouse right clicks" via the SendInpuut function (mouse emulation works fine). I am also trying to make this project capable of "Rearrange the keyboard" (example type "e" and "K" is sent to the application) by using SendInput function and I am not able to get it to work. I am finding the use of SendInput...
79
5323
by: pinkfloydhomer | last post by:
I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this: # start import sys numChars = 0 startCode = 0 count = 0
3
4495
by: Rick Strahl [MVP] | last post by:
I'm working on an app that's using the WebBrowser control. I got the control working fine, hooking to the document object. But I've run into a major issue with hooking the Document events. Whenever I hook any of the HTMLDocumnetEvent2_Event events like this: HTMLDocumentEvents2_Event DocEvents = this.Browser.Document as HTMLDocumentEvents2_Event ; DocEvents.oncontextmenu += new...
5
2026
by: Ryan Ginstrom | last post by:
Apropos recent threads about GUI editors, coming from a Win32/WTL C++ background, I actually like the idea of being able to (easily) create GUIs programmatically. But I still see a lot of the same tedium: hooking up events to handlers, and getting data into and out of dialogs. In C++, this is generally handled through code generation and/or macros, but IMO these are brittle and ugly. So my question: Is there a Pythonic way to make...
8
1985
by: pigeonrandle | last post by:
Hi, Has anyone had any experience with hooking messages in other application windows (like SPY++). I want to listen for WM_MOVE messages, but can only seem to find examples of Keyboard and Mouse hooks. Please (and thankyou), James Randle.
0
3743
by: joemango | last post by:
Hi everyone, I was just reading the following artilce titled: "Spy: A Windows CE API Interceptor" by Dmitri Leman in Dr. Dobbs Journal, September 02, 2003 located at http://www.ddj.com/184405459?pgno=1 the article presents source code that includes a dll file which holds all the hooking procedures it is written in C++. Is there an implementation for windows api hooking in C# or .NET out
1
1296
by: Warren Stringer | last post by:
Here is what I would like to do: #------------------------------------------------------------ a = Tr3() # implements domain specific language a.b = 1 # this works, Tr3 overrides __getattr__ a.__dict__ = 2 # just so you know that b is local a = 3 # I want to resolve locally, but get: Traceback (most recent call last): ... exec cmd in globals, locals ...
0
9841
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
11260
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
10858
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
10484
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
8036
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
7187
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
5881
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
6077
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4296
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.