Subclassing (Spy++ alike)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to write a functionality to catch all Windows Messages from any
Window (like spy++ does). I thought that would be possible by subclassing the
window.

So I wrote a class that inherts from NativeWindow, Assigned a Handle from
another window and overrode the WndProc method.

BUT, I never catch any message EXCEPT if I assign the handle of my own
application's window...

Has anyone done something like this before?

I'm stuck at the moment....

Regards Alexander
 
So,

I have tried to do some basic steps in windows hooking and wrote the
following code. This works fine, at least for some seconds, then the
application freezes and the explorer process is killed =D, what am I doing
wrong?

The hook callback delegate:
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
setting up the hook:
HookProc p = new HookProc(this.CallbackFunction);
this.hook = SetWindowsHookEx(HookType.WH_CALLWNDPROC, p,
Marshal.GetHINSTANCE(typeof(Class1).Module),0);
Callback implementation:
private int CallbackFunction(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0)
{
return CallNextHookEx(this.hook, code, wParam, lParam);
}
int ret;
try
{
Message msg = (Message)Marshal.PtrToStructure(lParam, typeof(Message));
Console.WriteLine(msg);
}
catch
{
Console.WriteLine("exception");
}
finally
{
ret = CallNextHookEx(this.hook, code, wParam, lParam);
}
return ret;
}

Regards Alexander
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top