Пример #1
0
        public void Start()
        {
            if(_disposed)
                throw new ObjectDisposedException("This object is disposed");

            if (!IsStarted &&
                _hookType != 0)
            {

                // Make sure we keep a reference to this delegate!
                // If not, GC randomly collects it, and a NullReference exception is thrown
                _hookCallback = new HookProc(HookCallbackProcedure);

                _handleToHook = SetWindowsHookEx(
                    _hookType,
                    _hookCallback,
                    Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
                    0);

                // Were we able to sucessfully start hook?
                if (_handleToHook != 0)
                {
                    IsStarted = true;
                }

            }

        }
Пример #2
0
 public Hook()
 {
     this.instance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]); this.threadID = 0;
     hookHandle = IntPtr.Zero;
     hookProcEx = hookProc;
     //SetHook();
 }
Пример #3
0
        public static void EnsureSubscribedToGlobalKeyboardEvents()
        {
            if (s_KeyboardHookHandle == 0)
            {
                //See comment of this field. To avoid GC to clean it up.
                s_KeyboardDelegate = KeyboardHookProc;

                s_KeyboardHookHandle = SetWindowsHookEx(
                      WH_KEYBOARD_LL,
                      s_KeyboardDelegate,
                      IntPtr.Zero,
                        // Marshal.GetHINSTANCE(                            Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);

                if (s_KeyboardHookHandle == 0)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
                    int errorCode = Marshal.GetLastWin32Error();
                    //do cleanup

                    //Initializes and throws a new instance of the Win32Exception class with the specified error.
                    throw new Win32Exception(errorCode);
                }
            }
        }
Пример #4
0
 public static IntPtr SetHook(int type, HookProc callback)
 {
     var process = Process.GetCurrentProcess();
     var module = process.MainModule;
     var handle = GetModuleHandle(module.ModuleName);
     return SetWindowsHookEx(type, callback, handle, 0);
 }
Пример #5
0
 public void Start()
 {
     // 安装键盘钩子
     if (hKeyboardHook == 0)
     {
         KeyboardHookProcedure = new HookProc(KeyboardHookProc);
         //GetModuleHandle 函数 替代 Marshal.GetHINSTANCE
         //防止在 framework4.0中 注册钩子不成功
         //IntPtr modulePtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
         //hKeyboardHook = SetWindowsHookEx(13, hookproc, modulePtr, 0);
         hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
         //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
         //************************************ 
         //键盘线程钩子 
         //SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId());//指定要监听的线程idGetCurrentThreadId(),
         //键盘全局钩子,需要引用空间(using System.Reflection;) 
         //SetWindowsHookEx( 13,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0); 
         // 
         //关于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函数将钩子加入到钩子链表中,说明一下四个参数: 
         //idHook 钩子类型,即确定钩子监听何种消息,上面的代码中设为2,即监听键盘消息并且是线程钩子,如果是全局钩子监听键盘消息应设为13, 
         //线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14。lpfn 钩子子程的地址指针。如果dwThreadId参数为0 或是一个由别的进程创建的 
         //线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何 
         //消息后便调用这个函数。hInstance应用程序实例的句柄。标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子 
         //程代码位于当前进程,hInstance必须为NULL。可以很简单的设定其为本应用程序的实例句柄。threaded 与安装的钩子子程相关联的线程的标识符
         //如果为0,钩子子程与所有的线程关联,即为全局钩子
         //************************************ 
         //如果SetWindowsHookEx失败
         if (hKeyboardHook == 0)
         {
             Stop();
             throw new Exception("安装键盘钩子失败");
         }
     }
 }
        private static void EnsureSubscribedToGlobalMouseEvents()
        {
            // install Mouse hook only if it is not installed and must be installed
            if (s_MouseHookHandle == 0)
            {
                //See comment of this field. To avoid GC to clean it up.
                s_MouseDelegate = MouseHookProc;
                //install hook
                var mar = LoadLibrary("user32.dll");
                s_MouseHookHandle = SetWindowsHookEx(
                    WH_MOUSE_LL,
                    s_MouseDelegate,
                    mar,
                    //Marshal.GetHINSTANCE(
                    //    Assembly.GetExecutingAssembly().GetModules()[0]),
                    0);
                //If SetWindowsHookEx fails.
                if (s_MouseHookHandle == 0)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
                    int errorCode = Marshal.GetLastWin32Error();
                    //do cleanup

                    //Initializes and throws a new instance of the Win32Exception class with the specified error.
                    throw new Win32Exception(errorCode);
                }
            }
        }
Пример #7
0
 private static IntPtr SetHook(int hookType, HookProc hookProc)
 {
     using (Process currentProcess = Process.GetCurrentProcess())
     using (ProcessModule currentModule = currentProcess.MainModule)
     {
         return NativeMethods.SetWindowsHookEx(hookType, hookProc, NativeMethods.GetModuleHandle(currentModule.ModuleName), 0);
     }
 }
Пример #8
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="HandleTo">Дескриптор окна, к которому хотим подключиться</param>
 public HookClass(IntPtr HandleTo)
 {
     weFinder = new WindowsEnumerator();
     weFinder.ParentHandle = HandleTo;
     if (HandleTo != IntPtr.Zero)
         this.ThreadId = GetProcess(HandleTo);
     KBCallbackDelegate = KBCallbackFunction;
 }
Пример #9
0
 public void SetHook()
 {
     _keyboardHookDelegate = KeyboardHookProc;
     var cProcess = Process.GetCurrentProcess();
     var cModule = cProcess.MainModule;
     var mh = GetModuleHandle(cModule.ModuleName);
     _hHook = SetWindowsHookEx(WhKeyboardLl, _keyboardHookDelegate, mh, 0);
 }
Пример #10
0
 internal HookBase(HookType type)
 {
     _hType = type;
     _hProc = new HookProc(HookProcBase);
     using (Process p = Process.GetCurrentProcess())
     using (ProcessModule pm = p.MainModule)
         _hHook = SetWindowsHookEx((int)_hType, _hProc, GetModuleHandle(pm.ModuleName), 0);
 }
Пример #11
0
 static MessageBoxEx()
 {
     HookProcdure = MessageBoxHookProc;
     HookTimer = MessageBoxTimerProc;
     _hookTimeout = 0;
     _hookCaption = null;
     _hHook = IntPtr.Zero;
 }
 static MessageBoxWithTimeout()
 {
     hookProc = new HookProc(MessageBoxHookProc);
     hookTimer = new TimerProc(MessageBoxTimerProc);
     hookTimeout = 0;
     hookCaption = null;
     hHook = IntPtr.Zero;
 }
Пример #13
0
 public CrossModeHook()
     : base(HookType.WH_MOUSE_LL)
 {
     m_filterFunc = new HookProc(this.MouseProc);
     timer = new Timer();
     timer.Interval = 250;
     timer.Tick += new EventHandler(timer_Tick);
 }
		public void SetHook()
		{
			MouseHookProcedure = new HookProc(MouseHookProc);
// ReSharper disable CSharpWarnings::CS0612
#pragma warning disable 618
			hHook = SetWindowsHookEx(WH_MOUSE, MouseHookProcedure, (IntPtr)0, AppDomain.GetCurrentThreadId());
#pragma warning restore 618
			// ReSharper restore CSharpWarnings::CS0612
		}
Пример #15
0
 private static void SetHook()//установление хука на клаву
 {
     if (s_KeyboardHookHandle == 0)
     {
         s_KeyboardDelegate = KeyboardHookProc;
         IntPtr hInstance = LoadLibrary("User32");
         s_KeyboardHookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, s_KeyboardDelegate, hInstance, 0);
     }
 }
Пример #16
0
 internal HookHandle(HookType hookType, HookProc hookProc) : base(IntPtr.Zero, false)
 {
     GCHandle.Alloc(hookProc);
     handle = User32.SetWindowsHookEx(hookType, hookProc, IntPtr.Zero, IntPtr.Zero);
     if (handle == IntPtr.Zero)
     {
         IsInvalid = true;
     }
 }
Пример #17
0
 public void SetActive()
 {
     keyHook = new HookProc(KeyHook);
     using (Process curProcess = Process.GetCurrentProcess())
     using (ProcessModule curModule = curProcess.MainModule)
     {
         hKeyHook = Win32Functions.SetWindowsHookEx(Win32Defines.WH_KEYBOARD_LL,
             keyHook, (IntPtr)Win32Functions.GetModuleHandle(curModule.ModuleName), 0);
     }
 }
Пример #18
0
 // 安装钩子
 public bool Start()
 {
     if (hKeyboardHook == 0)
     {
         hookproc = new HookProc(KeyboardHookProc);
         //WH_KEYBOARD_LL = 13
         hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
     }
     return (hKeyboardHook != 0);
 }
Пример #19
0
 public EveTask(EveEntry _entry, refresh r)
 {
     _ref = r;
     entry = _entry;
     this.handler = entry.handler;
     this.startPoint = new Point(0, 0);
     this.endPoint = new Point(0, 0);
     this.period = 300;
     this.timeLeft = 0;
     EveHookProcedure = new HookProc(MyCallbackFunction);
 }
Пример #20
0
 private static IntPtr SetHook(HookProc proc)
 {
     using (Process curProcess = Process.GetCurrentProcess())
     using (ProcessModule curModule = curProcess.MainModule)
     {
         return SetWindowsHookEx(WH_KEYBOARD_LL,
                                 proc,
                                 GetModuleHandle(curModule.ModuleName),
                                 0);
     }
 }
        public FrmWindowPicker()
        {
            InitializeComponent();
            _tool = new Tool();

            _mouseHookProc = Mouse_HookCallback;
            SetHook(WH_KEYBOARD_LL);

            MouseActionProcess mouseActionProcess = new MouseActionProcess();
            // set local mouse event handler
            MouseAction += mouseActionProcess.MouseAction;
        }
Пример #22
0
        public static void SetHook()
        {
            if (hHook == 0)
            {
                MouseHookProcedure = new HookProc(MouseHookProc);

                hHook = SetWindowsHookEx(WH_MOUSE_LL,
                                          MouseHookProcedure,
                                          (IntPtr)0,
                                          0);
            }
        }
Пример #23
0
 public void Start()
 {
    if(isRunning())
    {
       this.Stop();
    }
    this.hookDeleg = new HookProc(this.HookProcedure);
    hHook = SetWindowsHookEx(20, this.hookDeleg, GetModuleHandle(null), 0);
    if(hHook == 0)
    {
       throw new SystemException("Failed acquiring of the hook.");
    }
 }
Пример #24
0
        ///<summary>
        ///Initialize the keylogger with a AggregatorClient to send keypresses too.
        ///</summary>
        public KeyLogger(KerPressAggergator.KerPressAggergatorClient c)
        {
            client = c;

            callback = new HookProc(HookCallback);

            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                _hookID = SetWindowsHookEx(WH_KEYBOARD_LL, this.callback,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }
Пример #25
0
 public static bool StartHook()
 {
     if (hHook == 0)
     {
         KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
         hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
                     KeyBoardHookProcedure,
                     //IntPtr.Zero,
                     GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName),
                     0);
     }
     return (hHook != 0);
 }
 public void SetHook()
 {
     this.kbCallbackDelegate = new HookProc(this.MyCallbackFunction);
     using (Process process = Process.GetCurrentProcess())
     {
         using (ProcessModule module = process.MainModule)
         {
             IntPtr hModule = NativeMethods.GetModuleHandle(module.ModuleName);
             mHookHandle = NativeMethods.SetWindowsHookEx(HookType.WH_KEYBOARD_LL, kbCallbackDelegate, hModule, 0);
             IsHooked = true;
         }
     }
 }
Пример #27
0
 private static void Subscribe()
 {
     if (handle == 0)
     {
         hookProc = MouseHookProc;
         handle = WinApi.SetWindowsHookEx(WH_MOUSE_LL, hookProc, IntPtr.Zero, 0);
         if (handle == 0)
         {
             int errorCode = Marshal.GetLastWin32Error();
             throw new Win32Exception(errorCode);
         }
     }
 }
Пример #28
0
        public KeyHook()
        {
            var modules = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
            var hModule = Marshal.GetHINSTANCE(modules[0]);

            _hookProc = new HookProc(OnHookKey);

            var WH_KEYBOARD_LL = 13;
            _hook = SetWindowsHookEx(WH_KEYBOARD_LL, _hookProc, hModule, 0);
            if (_hook == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Пример #29
0
        protected override void OnLoad(EventArgs e)
        {
            if (!DesignMode)
            {
                MouseHookProcedure = MouseHookProc;

                hHook = User32.SetWindowsHookEx(Constants.WH_MOUSE, MouseHookProcedure, (IntPtr) 0, Kernel32.GetCurrentThreadId());

                if (hHook == 0)
                    Console.Write("Fail to hook window");
            }

            base.OnLoad(e);
        }
Пример #30
0
 public static extern IntPtr SetWindowsHookEx(HookType code,
                                              HookProc func,
                                              IntPtr hInstance,
                                              int threadID);
 static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
Пример #32
0
 public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
Пример #33
0
 static MessageBoxManager()
 {
     hookProc = new HookProc(MessageBoxHookProc);
     enumProc = new EnumChildProc(MessageBoxEnumProc);
     hHook    = IntPtr.Zero;
 }
Пример #34
0
 private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
Пример #35
0
 public HookingManager()
 {
     _proc = HookCallback;
 }
Пример #36
0
 static MessageBoxManager()
 {
     hookProc = MessageBoxHookProc;
     enumProc = MessageBoxEnumProc;
     hHook    = IntPtr.Zero;
 }
Пример #37
-1
 protected GlobalHook(HookTypes hookType)
 {
     this.mHookType = HookTypes.NONE;
     this.mHandle = IntPtr.Zero;
     this.mProc = new HookProc(this.OnProc);
     this.mHookType = hookType;
 }