public void SetHook()
        {
            lock (lockObject)
            {
                if (IsActivated)
                {
                    throw new InvalidOperationException();
                }
                var log = new CallLogger("SetWindowsHookEx");
                log.Add("Hook type: {0}", Enum.GetName(typeof(HookType), hookType));
                var hInstance = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

                log.Add("hInstance: 0x{0:X}", hInstance.ToInt64());
                hHook = SetWindowsHookEx((int)hookType, systemCallback, hInstance, 0);
                if (IsActivated)
                {
                    log.Add("hHook: 0x{0:X}", hHook.ToInt64());
                    log.Success();
                }
                else
                {
                    log.FailWithErrorCode();
                }
            }
        }
 public void Unhook()
 {
     lock (lockObject)
     {
         if (!IsActivated)
         {
             throw new InvalidOperationException();
         }
         var log = new CallLogger("UnhookWindowsHookEx");
         log.Add("Hook type: {0}", Enum.GetName(typeof(HookType), hookType));
         log.Add("hHook: 0x{0:X}", hHook);
         if (UnhookWindowsHookEx(hHook))
         {
             log.Success();
         }
         else
         {
             log.FailWithErrorCode();
         }
         hHook = IntPtr.Zero;
     }
 }
Beispiel #3
0
        protected void Send(INPUT[] input)
        {
            var log = new CallLogger("SendInput");

            foreach (var item in input.Select((v, i) => new { v, i }))
            {
                var inputType = (InputType)item.v.type;
                if (inputType.Equals(InputType.INPUT_MOUSE))
                {
                    var data      = item.v.data.asMouseInput;
                    var eventType = (MouseEventType)data.dwFlags;

                    log.Add("MouseEvent[{0}]:", item.i);
                    log.Add("dx: {0}", data.dx);
                    log.Add("dy: {0}", data.dy);
                    log.Add("dwFlags: {0} | {1}", eventType, ToHexString(data.dwFlags));
                    if (eventType.HasFlag(MouseEventType.MOUSEEVENTF_XDOWN | MouseEventType.MOUSEEVENTF_XUP))
                    {
                        log.Add("mouseData: {0} | {1}", (XButtonType)data.mouseData.asXButton.type, ToHexString((uint)data.mouseData.asXButton.type));
                    }
                    else if (eventType.HasFlag(MouseEventType.MOUSEEVENTF_WHEEL | MouseEventType.MOUSEEVENTF_HWHEEL))
                    {
                        log.Add("mouseData: {0} | {1}", data.mouseData.asWheelDelta.delta, ToHexString((uint)data.mouseData.asWheelDelta.delta));
                    }
                    log.Add("dwExtraInfo: {0}", ToHexString(data.dwExtraInfo.ToUInt64()));
                }
                else if (inputType.HasFlag(InputType.INPUT_KEYBOARD))
                {
                    var data      = item.v.data.asKeyboardInput;
                    var eventType = (KeyboardEventType)data.dwFlags;
                    log.Add("KeyboardEvent[{0}]:", item.i);
                    log.Add("wVk: {0}", data.wVk);
                    log.Add("wScan: {0}", data.wScan);
                    log.Add("dwFlags: {0} | {1}", eventType, ToHexString(data.dwFlags));
                    log.Add("dwExtraInfo: {0}", ToHexString(data.dwExtraInfo.ToUInt64()));
                }
            }
            if (SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) > 0)
            {
                log.Success();
            }
            else
            {
                log.FailWithErrorCode();
            }
        }