访问冲突读取位置0x0000000c

时间:2010-07-08 14:30:04

标签: c# winforms winapi

在我的应用程序中,我使用'LowLevelKeyboardProc'禁用了开始菜单。但是当我连续按Tab键时出现以下错误。 错误..

  

0x00352d58处的未处理异常   Timer_soosai.exe:0xC0000005:访问   违规读取位置0x0000000c。

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

导致错误解除。* /

守则:

[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);

        }

在这种情况下,有谁可以帮助我。非常感谢提前..

3 个答案:

答案 0 :(得分:3)

你的P / Invoke声明都是错误的,使用“int”,其中需要IntPtr。这段代码确实会在64位版本的Windows上轰炸。通过从pinvoke.net获取正确的声明来解决这些问题

答案 1 :(得分:1)

看起来你在某处解除引用NULL(即零指针)。虽然NULL不是正式零,但它实际上是。 (这不是零指针的实际解引用,因为代码假设从那里开始有一个结构并且正在访问该结构的第四个单词。)

检查您使用null或zero参数调用代码的任何位置。还要考虑将对低级API的所有调用都设置为私有,并使用保护所有参数的安全版本对其进行包装。我无法诊断出更多;你还没有提供足够的信息(好吧, me 至少这样做;有经验的人可能会提供帮助)。

答案 2 :(得分:-1)

当程序多次访问了不能访问的内存空间(未分配的OR不属于该进程)或内存损坏(尽管指针错误,但多次读写)时,发生此异常。如果您想进一步了解此异常的原因,请参见System.AccessViolationException

,如果您只想处理它,请参见此答案How to Handle Access Violation Exception