473,320 Members | 1,945 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,320 software developers and data experts.

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.HOOKPROC(MsgHookProc);

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

uint processId = 0;

threadId = API.GetWindowThreadProcessId(taskBarHandle, out processId);

if(threadId != 0)

hookResult = API.SetWindowsHookEx(API.WH_GETMESSAGE,
msgHookProc,Marshal.GetHINSTANCE((typeof(Form1).Mo dule)), threadId);

Console.WriteLine("Hook Result = " + hookResult.ToString());

}

....

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

{

if(nCode < 0 )

return API.CallNextHookEx(hookResult, nCode, wParam, lParam);
API.MSG message = (API.MSG) Marshal.PtrToStructure(lParam, typeof(API.MSG));

MessageBox.Show(message.pt.x.ToString());
return API.CallNextHookEx(hookResult, nCode, wParam, lParam);

}


Nov 15 '05 #1
1 2386
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****************@TK2MSFTNGP10.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.HOOKPROC(MsgHookProc);

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

uint processId = 0;

threadId = API.GetWindowThreadProcessId(taskBarHandle, out processId);

if(threadId != 0)

hookResult = API.SetWindowsHookEx(API.WH_GETMESSAGE,
msgHookProc,Marshal.GetHINSTANCE((typeof(Form1).Mo dule)), threadId);

Console.WriteLine("Hook Result = " + hookResult.ToString());

}

...

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

{

if(nCode < 0 )

return API.CallNextHookEx(hookResult, nCode, wParam, lParam);
API.MSG message = (API.MSG) Marshal.PtrToStructure(lParam, typeof(API.MSG));
MessageBox.Show(message.pt.x.ToString());
return API.CallNextHookEx(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
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...
0
by: Jim Hubbard | last post by:
Is GDI Hooking possible in VB.Net?
3
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...
1
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...
79
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
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....
5
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...
8
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...
0
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 ...
1
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,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.