简体   繁体   English

如何正确摘机?

[英]How to UnHook properly?

I made a very simple Hook codes (I'm a beginner). 我做了一个非常简单的Hook代码(我是初学者)。

I opened Notepad and tested. 我打开记事本并进行了测试。

If I press ANY key it make a beep and printed itself. 如果按ANY键,则会发出哔声并自行打印。

Except "x" key, it is a terminator key. 除“ x”键外,它是终结符。

Question : 问题

I do not want to see "x" key printed. 我不想看到“ x”键被打印出来。 I just quit the program. 我刚退出程序。 What do I have to do ? 我需要做什么 ?

namespace HookingStudy
{
    class HookingClass          
    {
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = hookCallBack;
        private static IntPtr _hookID = IntPtr.Zero;                    
        public static void Main()
        {
            Beep(1111, 222);
            _hookID = SetHook(_proc);
            Application.Run();
        }
        private static IntPtr hookCallBack(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if( nCode >= 0 && wParam == (IntPtr) WM_KEYDOWN )
            {
                int vkCode = Marshal.ReadInt32(lParam);
                if( vkCode.ToString() == "88" )                 //   88 ("x" key)
                {
                    Beep(7777, 222);
                    UnhookWindowsHookEx(_hookID);     
                    Process.GetCurrentProcess().Kill(); 
                }
                Beep(2222, 55);
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);  
        }
        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using( Process curProcess = Process.GetCurrentProcess() )
            using( ProcessModule curModule = curProcess.MainModule )
            {
                return SetWindowsHookEx(13, proc, GetModuleHandle(curModule.ModuleName), 0);
            }                   
        }
        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("KERNEL32.DLL")]                             
        extern public static void Beep(int freq, int dur);      
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    }  
}

I do not want to see the terminator x printed at Notepad 我不想在记事本上看到终止符x

Then do not call next hook in chain: 然后不要调用链中的下一个钩子:

return CallNextHookEx(_hookID, nCode, wParam, lParam);

The idea of hooking it to install own handler prior existing handlers (afair from winapi). 挂钩它的想法是先安装自己的处理程序, 然后再安装现有的处理程序(与winapi不同)。 By intercepting (like you are doing it already) you are not only listening , but still invoking previous handlers with that call. 通过拦截(就像您已经在做的那样),您不仅在侦听 ,而且还在通过该调用调用以前的处理程序。

Try something like (untested): 尝试类似(未试用)的方法:

if( vkCode == 88)
{
    ...
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM