469,140 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

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 2210
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jim Hubbard | last post: by
3 posts views Thread by Seth Gecko | last post: by
1 post views Thread by Matthew Kelly | last post: by
79 posts views Thread by pinkfloydhomer | last post: by
3 posts views Thread by Rick Strahl [MVP] | last post: by
5 posts views Thread by Ryan Ginstrom | last post: by
8 posts views Thread by pigeonrandle | last post: by
reply views Thread by joemango | last post: by
1 post views Thread by Warren Stringer | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.