Access violation reading location 0x0000000c

3

In my application I disable the Start menu using 'LowLevelKeyboardProc'. But i get the following error when i pressing the tab key continuously. The Error..

Unhandled exception at 0x00352d58 in Timer_soosai.exe: 0xC0000005: Access violation reading location 0x0000000c.

00352D58  mov         eax,dword ptr [ecx+0Ch] /*This is the line which

causes the error in disassemply.*/

The Code:

[DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId);
        [DllImport("user32", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int UnhookWindowsHookEx(int hHook);
        public delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        [DllImport("user32", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        public const int WH_KEYBOARD_LL = 13;

        /*code needed to disable start menu*/
        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);

        private const int SW_HIDE = 0;
        private const int SW_SHOW = 1;
        public struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        public static int intLLKey;

        public int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;

            switch (wParam)
            {
                case 256:
                case 257:
                case 260:
                case 261:
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key,
                    blnEat = ((lParam.vkCode == 9) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 0)) | ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1)) | ((lParam.vkCode == 73) && (lParam.flags == 0));
                    break;
            }

            if (blnEat == true)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(0, nCode, wParam, ref lParam);
            }
        }
        public void KillStartMenu()
        {
            int hwnd = FindWindow("Shell_TrayWnd", "");
            ShowWindow(hwnd, SW_HIDE);
        }

    private void Login_Load(object sender, EventArgs e)
        {
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);

        }

Can anyone please help me in this case. Many thanks in advance..

c#
winforms
winapi
asked on Stack Overflow Jul 8, 2010 by ASr.. • edited Jul 8, 2010 by AndersK

3 Answers

3

Your P/Invoke declarations are all wrong, using "int" where IntPtr is required. This code is indeed going to bomb on a 64-bit version of Windows. Get them fixed by getting the proper declarations from pinvoke.net

answered on Stack Overflow Jul 8, 2010 by Hans Passant
1

Looks like you're dereferencing a NULL (i.e., a zero pointer) somewhere. While a NULL isn't formally a zero, it is in practice. (It's not an actual dereference of a zero pointer because the code is assuming there's a struct starting there and is accessing the 4th word of that struct.)

Check for anywhere where you're calling the code with a null or zero parameter. Also consider making all calls into the low-level API private and wrap them with safe versions that guard all the arguments. I can't diagnose much more than that; you don't provide quite enough info yet (well, for me to do so at least; someone more experienced may be able to help).

answered on Stack Overflow Jul 8, 2010 by Donal Fellows
-1

this exception occurred when the program access the memory space that must not be accessed (Not Allocated OR is not belong to the process) several times or when the memory is corrupted (several read-write though bad pointers). If you want to know more about the cause of this exception see System.AccessViolationException.

and if you just want to handle it, see this answer How to Handle Access Violation Exception

answered on Stack Overflow Mar 17, 2020 by Mamo Ghandi

User contributions licensed under CC BY-SA 3.0