Prev: WPF: Localization and IValueConverter
Next: Rot13
From: Kerem Gümrükcü on 30 Jan 2009 14:02 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 -- -- ----------------------- Beste Gr�sse / Best regards / Votre bien devoue Kerem G�mr�kc� Latest Project: http://www.pro-it-education.de/software/deviceremover Latest Open-Source Projects: http://entwicklung.junetz.de ----------------------- "This reply is provided as is, without warranty express or implied."
From: Angel J. Hernández M. on 3 Feb 2009 00:49 Hi there, I think the issue here might be that the GC (Garbage Collector) is collecting your callbacks while your application still is using them. I wrote a post last year about this http://msmvps.com/blogs/angelhernandez/archive/2008/10/04/accessing-a-managed-object-from-unmanaged-memory-native.aspx Hope this helps, Cheers, -- Angel J. Hern�ndez M MCP,MCAD,MCSD,MCDBA Microsoft MVP http://msmvps.com/blogs/angelhernandez *************************************************** Technical Solution Architect http://www.customware.net "Kerem G�mr�kc�" <kareem114(a)hotmail.com> wrote in message news:#Zlhh1wgJHA.2384(a)TK2MSFTNGP04.phx.gbl... > 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 > > -- > -- > ----------------------- > Beste Gr�sse / Best regards / Votre bien devoue > Kerem G�mr�kc� > Latest Project: http://www.pro-it-education.de/software/deviceremover > Latest Open-Source Projects: http://entwicklung.junetz.de > ----------------------- > "This reply is provided as is, without warranty express or implied."
|
Pages: 1 Prev: WPF: Localization and IValueConverter Next: Rot13 |