SetWindowsHookEx (keylogger app) - Memory Access Violation
-
Hi, I wrote a piece of code to monitor keyword activities (just for fun and testing this api func) I want to know what is wrong? sometime I get this error: {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} Stack trace: " at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)\r\n at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run()\r\n at hook.Program.Main() in C:\\Documents and Settings\\Nazila\\My Documents\\Visual Studio 2005\\Projects\\hook\\hook\\Program.cs:line 19" my code is :
public class KeyPosition { static public Int16 WM_KEYDOWN = 0x0100; static public Int16 WM_KEYUP = 0x0101; static public Int16 WM_SYSKEYDOWN = 0x0104; static public Int16 WM_SYSKEYUP = 0x0105; } delegate int HookProc(int code, IntPtr wParam, ref keyboardHookStruct lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] static extern IntPtr SetWindowsHookEx(HookType hook, HookProc callback, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref keyboardHookStruct lParam); [DllImport("kernel32.dll")] static extern IntPtr LoadLibrary(string lpFileName); 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 } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto,Pack=1)] public struct keyboardHookStruct { public int vkCode; publ