protected override void HandleHook(HookCode nCode, IntPtr wParam, IntPtr lParam)
        {
            var mouseMessageIdentifier     = (LowLevelMouseMessageIdentifier)wParam.ToInt32();
            var lowlevelMouseHookStructure = Marshal.PtrToStructure <MSLLHOOKSTRUCT>(lParam);

            this.Handle(mouseMessageIdentifier, lowlevelMouseHookStructure);
        }
        protected override void HandleHook(HookCode nCode, IntPtr wParam, IntPtr lParam)
        {
            var keyboardMessageIdentifier     = (LowLevelKeyboardMessageIdentifier)wParam.ToInt32();
            var lowLevelKeyboardHookStructure = Marshal.PtrToStructure <KBDLLHOOKSTRUCT>(lParam);

            this.Handle(keyboardMessageIdentifier, lowLevelKeyboardHookStructure);
        }
Esempio n. 3
0
        private long Listener(
            HookCode nCode,
            WindowsMessage wParam,
            MsLlHookStruct lParam)
        {
            if (this.actions.TryGetValue(wParam, out var action))
            {
                this.sync.Post(state => action(), null);
            }

            return(User32.CallNextHookEx(
                       nCode: nCode,
                       wParam: wParam,
                       lParam: lParam));
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int KeyboardHookProc(HookCode nCode, IntPtr wParam, IntPtr lParam)
        {
            if ((nCode == HookCode.Action) && (KeyDetected != null))
            {
                var khs  = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                var args = new KeyDataEventArgs((WM)((int)wParam), khs.VkCode, khs.ScanCode, khs.Time);

                KeyDetected(this, args);

                if (args.Handled)
                {
                    return(1);
                }
            }

            return(NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam));
        }
Esempio n. 5
0
        protected virtual IntPtr LowLevelKeyboardHookProc(HookCode hc, KeyEvent keyEvent, KeyEventInfo info)
        {
            try
            {
                string line = "\r\nHC: [" + hc.ToString() + "]\r\nKEY_EVENT: [" + keyEvent.ToString() + "]\r\n" + info.ToString();
                LocalStaticLogger.WriteLine(line);
                Console.Out.WriteLine(line);

                foreach (var remappedKey in mappings.Keys)
                {
                    // we fudge equality here, comparing a KeyPress to a KeyEvent, technically
                    // they are not so much equal as equivalent
                    if (info == remappedKey)
                    {
                        var mappedKey = mappings[remappedKey];
                        if (mappedKey != null)
                        {
                            if (keyEvent == KeyEvent.WM_KEYDOWN)
                            {
                                if (KeyEventSimulator.SimulateKeyDownAsync(mappedKey.VkCode, mappedKey.ScanCode, mappedKey.Extended))
                                {
                                    return(LowLevelKeyboardHook.HANDLED);
                                }
                            }
                            else if (keyEvent == KeyEvent.WM_KEYUP)
                            {
                                if (KeyEventSimulator.SimulateKeyUpAsync(mappedKey.VkCode, mappedKey.ScanCode, mappedKey.Extended))
                                {
                                    return(LowLevelKeyboardHook.HANDLED);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LocalStaticLogger.WriteLine(e.ToString());
            }

            return(IntPtr.Zero);
        }
Esempio n. 6
0
        /// <summary>
        /// If <paramref name="nCode"/> is &lt; 0 <see cref="HandleHook"/> will not be called.
        /// When overridden in a derived class, the implementor should ensure to return the value returned by <see cref="CallNextHook"/>.
        /// When overridden in a derived class and this function returns ea value lower than IntPtr.Zero and <see cref="CallNextHook"/> was not called,
        /// the message will be omitted.
        /// E.g. Hooking low level keyboard actions and returning -1 without calling <see cref="CallNextHook"/> can lead to suppressing all key events for the whole system.
        /// </summary>
        /// <returns></returns>
        protected virtual IntPtr NextHook(HookCode nCode, IntPtr wParam, IntPtr lParam)
        {
            this.CheckDisposed();

            if (nCode >= 0)
            {
                try
                {
                    this.HandleHook(nCode, wParam, lParam);
                }
                catch (Exception)
                {
                    // TODO: Implement handling if i know how to handle such a case.

                    Debugger.Break();
                }
            }

            return(this.CallNextHook(nCode, wParam, lParam));
        }
Esempio n. 7
0
        protected IntPtr CallNextHook(HookCode nCode, IntPtr wParam, IntPtr lParam)
        {
            this.CheckDisposed();

            return(NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
Esempio n. 8
0
 /// <summary>
 /// When overridden in a derived class, handles the hook.
 /// </summary>
 protected abstract void HandleHook(HookCode nCode, IntPtr wParam, IntPtr lParam);
Esempio n. 9
0
 public static extern IntPtr CallNextHookEx(IntPtr hhk, HookCode nCode, IntPtr wParam, IntPtr lParam);
        public static unsafe IntPtr Inject(HookCode nCode, IntPtr wparam, IntPtr lparam)
        {
            if (nCode != HookCode.HC_ACTION)
            {
                return(CallNextHookEx(IntPtr.Zero, nCode, wparam, lparam));
            }
            CWPSTRUCT *msg = (CWPSTRUCT *)lparam;

            OutputDebugString($"Got '{msg->message}' message");
            if (msg != null && msg->message == launchMessage)
            {
                OutputDebugString($"Got '{launchMessageName}' message");

                char *acmRemote = (char *)msg->wParam;

                var acmLocal = new string(acmRemote);
                OutputDebugString($"acmLocal = {acmLocal}");
                var acmSplit = acmLocal.Split('$');

                OutputDebugString($"About to load assembly {acmSplit[0]}");
                var assemblyContent = File.ReadAllBytes(acmSplit[0]);
                var assembly        = Assembly.Load(assemblyContent);
                if (assembly != null)
                {
                    OutputDebugString($"About to load type {acmSplit[1]}");
                    var type = assembly.GetType(acmSplit[1]);
                    if (type != null)
                    {
                        OutputDebugString($"Just loaded the type {acmSplit[1]}");
                        var methodInfo = type.GetMethod(acmSplit[2], BindingFlags.Static | BindingFlags.Public);
                        if (methodInfo != null)
                        {
                            OutputDebugString($"About to invoke {methodInfo.Name} on type {acmSplit[1]}");
                            var returnValue = methodInfo.Invoke(null, null);
                            if (returnValue == null)
                            {
                                returnValue = "NULL";
                            }
                            OutputDebugString($"Return value of {methodInfo.Name} on type {acmSplit[1]} is {returnValue}");
                            var injectedModule = GetModuleHandle("ManagedInjector.dll");
                            if (injectedModule != IntPtr.Zero)
                            {
                                OutputDebugString("Got 'ManagedInjector' module");
                                var kernel32Module = GetModuleHandle("kernel32.dll");
                                if (kernel32Module != IntPtr.Zero)
                                {
                                    OutputDebugString("Got 'kernel32' module");
                                    var addr = GetProcAddress(kernel32Module, "FreeLibrary");
                                    if (addr != IntPtr.Zero)
                                    {
                                        OutputDebugString("Got FreeLibrary address");
                                        uint threadId;
                                        var  thread = CreateThread(IntPtr.Zero, IntPtr.Zero, addr, injectedModule, CREATE_SUSPENDED, out threadId);
                                        if (thread != 0)
                                        {
                                            OutputDebugString("Thread successfully created");
                                            *(uint *)(msg->lParam) = threadId;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(CallNextHookEx(IntPtr.Zero, nCode, wparam, lparam));
        }
Esempio n. 11
0
 internal static extern int CallNextHookEx(IntPtr hhk, HookCode nCode, IntPtr wParam, IntPtr lParam);
Esempio n. 12
0
 internal static extern int CallNextHookEx(IntPtr hhk, HookCode nCode, IntPtr wParam, IntPtr lParam);
 public static extern long CallNextHookEx(
     [In][Optional] IntPtr hHk,
     [In] HookCode nCode,
     [In] WindowsMessage wParam,
     [In] MsLlHookStruct lParam
     );