Access Violation when SetWindowHookEx Hook-Procedure runs after a while,...

K

Kerem Gümrükcü

Hi,

can someone please tell me why this class i created runs into
a access violation after some while running it. It throws the exception
after a unsepcified amount if time. Its pretty a pointer problem, but
where do i have to check what?

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct CWPSTRUCT
{
public IntPtr lParam;
public IntPtr wParam;
public int message;
public IntPtr hwnd;
}

public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 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_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}

public delegate int HookProc(int code, IntPtr wParam, IntPtr
lParam);

public delegate int HookProcCWPS(int code, IntPtr wParam, ref
CWPSTRUCT cwps);

[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();

[DllImport("user32.dll", SetLastError = true,CharSet =
CharSet.Unicode)]
public static extern IntPtr SetWindowsHookEx(HookType hook,
HookProcCWPS callback,
IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern IntPtr SetWindowsHookEx(HookType hook, HookProc
callback,
IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam,
IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam,
ref CWPSTRUCT cwps);

[DllImport("user32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);

public class DRWindowHelperClass
{
private IntPtr windowHandle;
private IntPtr hhk;
private Exception exception;

public delegate void WindowMessageArrivedDelegate(ref Message
WindowMessage);
public event WindowMessageArrivedDelegate WindowMessageArrived;

public DRWindowHelperClass(IntPtr WindowHandle)
{
this.windowHandle = WindowHandle;
}

public Exception LastException
{
set
{
this.exception = value;
}

get
{
return this.exception;
}
}

public bool SetWindowsHook()
{
try
{
this.hhk =
DRWin32APIClass.SetWindowsHookEx(DRWin32APIClass.HookType.WH_CALLWNDPROC,
this.WndHookProcWPCS, IntPtr.Zero, DRWin32APIClass.GetCurrentThreadId());

if (this.hhk == IntPtr.Zero)
{
throw new Win32Exception();
}

this.exception = new Win32Exception(0);
return true;
}
catch (Exception err)
{
this.exception = err;
return false;
}
}

private int WndHookProcWPCS(int code, IntPtr wParam, ref
DRWin32APIClass.CWPSTRUCT cwps)
{
if (code < 0)
{
return DRWin32APIClass.CallNextHookEx(IntPtr.Zero, code,
wParam, ref cwps);
}
else
{
if (this.WindowMessageArrived != null)
{

Message m = new Message();
m.HWnd = cwps.hwnd;
m.LParam = cwps.lParam;
m.WParam = cwps.lParam;
m.Msg = cwps.message;
m.Result = new IntPtr(0);
this.WindowMessageArrived(ref m);
}

return DRWin32APIClass.CallNextHookEx(IntPtr.Zero, code,
wParam, ref cwps);
}
}

public bool RemoveWindowsHook()
{
try
{
if (DRWin32APIClass.UnhookWindowsHookEx(this.hhk) == true)
{
this.exception = new Win32Exception(0);
return true;
}
else
{
throw new Win32Exception();
}
}
catch (Exception err)
{
this.exception = err;
return false;
}
}

~DRWindowHelperClass()
{
if (this.hhk != IntPtr.Zero)
{
DRWin32APIClass.UnhookWindowsHookEx(this.hhk);
}
}

public IntPtr WindowHandle
{
get
{
return this.windowHandle;
}
}
}

Regards

Kerem
 

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

Top