Esempio n. 1
0
        public static int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr LPARAM)
        {
            ProcStructs.KBDLLHOOKSTRUCT kBDLLHOOKSTRUCT = (ProcStructs.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(LPARAM, typeof(ProcStructs.KBDLLHOOKSTRUCT));

            Console.WriteLine("LowLevelKeyboardProc Invoked");
            return((int)WinAPIs.CallNextHookEx(RunningDLLInstance, nCode, wParam, LPARAM));
        }
Esempio n. 2
0
        public static ProcStructs.MSG GetMessage(IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax)
        {
            ProcStructs.MSG msg = new ProcStructs.MSG();

            WinAPIs.GetMessage(out msg, hWnd, wMsgFilterMin, wMsgFilterMax);
            return(msg);
        }
Esempio n. 3
0
        public ProcStructs.WINDOWINFO GetWindowInfo1(IntPtr hWnd)
        {
            ProcStructs.WINDOWINFO wInfo = new ProcStructs.WINDOWINFO();
            wInfo.cbSize = (uint)Marshal.SizeOf(wInfo);

            WinAPIs.GetWindowInfo(hWnd, ref wInfo);
            return(wInfo);
        }
Esempio n. 4
0
        public static int CallWndProc(int nCode, IntPtr wParam, IntPtr LPARAM)
        {
            ProcStructs.CWPSTRUCT cWPSTRUCT = (ProcStructs.CWPSTRUCT)Marshal.PtrToStructure(LPARAM, typeof(ProcStructs.CWPSTRUCT));


            Console.WriteLine("CallWndProc Invoked");
            return((int)WinAPIs.CallNextHookEx(RunningDLLInstance, nCode, wParam, LPARAM));
        }
Esempio n. 5
0
        public string GetWindowText(IntPtr hWnd)
        {
            // Allocate correct string length first
            int           length = WinAPIs.GetWindowTextLength(hWnd);
            StringBuilder sb     = new StringBuilder(length + 1);

            WinAPIs.GetWindowText(hWnd, sb, sb.Capacity);
            return(sb.ToString());
        }
Esempio n. 6
0
        public static void OnMouseLeftClick(object sender, MouseEventArgs m)
        {
            #region XPath
            string labelText = string.Empty;

            Form1.textBox.Text = "Resolving Xpath";
            Stopwatch s1 = new Stopwatch();
            s1.Start();
            Task resolveData = Task.Factory.StartNew(() =>
            {
                System.Windows.Point point = new System.Windows.Point(m.X, m.Y);
                AutomationElement el       = AutomationElement.FromPoint(point);

                var processExecutablePath = WinAPIs.GetMainModuleFilepath(el.Current.ProcessId);
                var processName           = Process.GetProcessById(el.Current.ProcessId).ProcessName;
                WinAPIs w            = new WinAPIs();
                UIELementActivity u  = new UIELementActivity();
                var hWindow          = WinAPIs.WindowFromPoint(m.Location);
                string clickedWindow = "Clicked window is : " + w.GetWindowText(hWindow);

                Console.WriteLine(clickedWindow);
                string rawXPath    = string.Empty;
                string preparedXml = string.Empty;

                #region uncomment to get from c++ library
                //Stopwatch s = new Stopwatch();
                //s.Start();
                //rawXPath = w.GetUiXPath(m.X, m.Y);
                //s.Stop();
                //Console.WriteLine("Elapsed Time is :" + s.ElapsedMilliseconds);
                //preparedXml = GenerateXPath.GetUITaskList(rawXPath);
                ////var xPath = GenerateXPath.GenerateXPathToUiElement(preparedXml);

                //labelText = clickedWindow + Environment.NewLine + String.Format("RawXPath is ={0}" + Environment.NewLine, preparedXml);
                #endregion

                #region own written code
                preparedXml      = GenerateXPath.GetUITaskList(el);
                TrackedAction t1 = new TrackedAction
                {
                    Id                   = Guid.NewGuid(),
                    CreatedAt            = DateTime.Now,
                    IdentificationDetail = preparedXml,
                    ApplicationPath      = processExecutablePath,
                    ProcessName          = processName
                };
                new CSVHelper().WriteRecord(t1);
                #endregion
                //Console.WriteLine("RawXPath is : " + preparedXml);
            });
            resolveData.Wait();

            //Task t = Task.Run(() => Program.c.SendMessage((m.X + "|" + m.Y)));
            Form1.textBox.Text = labelText + Environment.NewLine + "Elapsed time in Resolving data is  message is :" + s1.ElapsedMilliseconds;
            #endregion
        }
Esempio n. 7
0
 public static void UnInstallHooks()
 {
     UnInstallResults.Add("WndProc", WinAPIs.UnhookWindowsHookEx(WndProc));
     UnInstallResults.Add("MsgProc", WinAPIs.UnhookWindowsHookEx(MsgProc));
     UnInstallResults.Add("KbProc", WinAPIs.UnhookWindowsHookEx(KbProc));
     UnInstallResults.Add("KbLLProc", WinAPIs.UnhookWindowsHookEx(KbLLProc));
     UnInstallResults.Add("MProc", WinAPIs.UnhookWindowsHookEx(MProc));
     UnInstallResults.Add("MLLProc", WinAPIs.UnhookWindowsHookEx(MLLProc));
     UnInstallResults.Add("MLLProc1", (int)WinAPIs.UninitializeHook(MLLProc1));
 }
Esempio n. 8
0
 public static void UnInstallWindowEventHooks()
 {
     UnInstallResults.Add("create", WinAPIs.UnhookWinEvent(hCreate));
     UnInstallResults.Add("destroy", WinAPIs.UnhookWinEvent(hDestroy));
     UnInstallResults.Add("show", WinAPIs.UnhookWinEvent(hShow));
     UnInstallResults.Add("select", WinAPIs.UnhookWinEvent(hSelect));
     UnInstallResults.Add("minimize", WinAPIs.UnhookWinEvent(hMinimize));
     UnInstallResults.Add("minimizeEnd", WinAPIs.UnhookWinEvent(hMinimizeEnd));
     UnInstallResults.Add("move", WinAPIs.UnhookWinEvent(hMoveSize));
     UnInstallResults.Add("moveEnd", WinAPIs.UnhookWinEvent(hMoveSizeEnd));
 }
Esempio n. 9
0
 public static void InstallWindowsEventHooks()
 {
     hCreate      = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_OBJECT_CREATE, Constants.EventConstants.EVENT_OBJECT_CREATE, Hooks.RunningDLLInstance, create, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hDestroy     = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_OBJECT_DESTROY, Constants.EventConstants.EVENT_OBJECT_DESTROY, Hooks.RunningDLLInstance, destroy, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hShow        = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_OBJECT_SHOW, Constants.EventConstants.EVENT_OBJECT_SHOW, Hooks.RunningDLLInstance, show, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hSelect      = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_OBJECT_SELECTION, Constants.EventConstants.EVENT_OBJECT_SELECTION, Hooks.RunningDLLInstance, select, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hMinimize    = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_SYSTEM_MINIMIZESTART, Constants.EventConstants.EVENT_SYSTEM_MINIMIZESTART, Hooks.RunningDLLInstance, minimize, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hMinimizeEnd = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_SYSTEM_MINIMIZEEND, Constants.EventConstants.EVENT_SYSTEM_MINIMIZEEND, Hooks.RunningDLLInstance, minimizeEnd, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hMoveSize    = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_SYSTEM_MOVESIZESTART, Constants.EventConstants.EVENT_SYSTEM_MOVESIZESTART, Hooks.RunningDLLInstance, move, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
     hMoveSizeEnd = WinAPIs.SetWinEventHook(Constants.EventConstants.EVENT_SYSTEM_MOVESIZEEND, Constants.EventConstants.EVENT_SYSTEM_MOVESIZEEND, Hooks.RunningDLLInstance, moveEnd, 0, 0, (uint)(Constants.Contexts.WINEVENT_INCONTEXT | Constants.Contexts.WINEVENT_SKIPOWNPROCESS));
 }
Esempio n. 10
0
        public static void InstallHooks()
        {
            WndHookProcedure     = new HookProc(CallWndProc);
            MsgHookProcedure     = new HookProc(GetMsgProc);
            KbHookProcedure      = new HookProc(KeyboardProc);
            KbLLHookProcedure    = new HookProc(LowLevelKeyboardProc);
            MouseHookProcedure   = new HookProc(MouseProc);
            MouseLLHookProcedure = new HookProc(LowLevelMouseProc);

            //WndProc = WinAPIs.SetWindowsHookEx(WindowsHookConstans.WH_CALLWNDPROC, WndHookProcedure, RunningDLLInstance, Thread.CurrentThread.ManagedThreadId);
            //if (WndProc == 0)
            //{
            //    int errorCode = Marshal.GetLastWin32Error();
            //    WinAPIs.UnhookWindowsHookEx(WndProc);
            //    throw new Win32Exception(errorCode);
            //}
            //MsgProc = WinAPIs.SetWindowsHookEx(Constants.WindowsHookConstans.WH_GETMESSAGE, MsgHookProcedure, RunningDLLInstance, 0);
            //if (MsgProc == 0)
            //{
            //    int errorCode = Marshal.GetLastWin32Error();
            //    WinAPIs.UnhookWindowsHookEx(MsgProc);
            //    throw new Win32Exception(errorCode);
            //}
            //KbProc = WinAPIs.SetWindowsHookEx(WindowsHookConstans.WH_KEYBOARD, KbHookProcedure, RunningDLLInstance, 0);
            //if (KbProc == 0)
            //{
            //    int errorCode = Marshal.GetLastWin32Error();
            //    WinAPIs.UnhookWindowsHookEx(KbProc);
            //    throw new Win32Exception(errorCode);
            //}
            //KbLLProc = WinAPIs.SetWindowsHookEx(Constants.WindowsHookConstans.WH_KEYBOARD_LL, KbLLHookProcedure, RunningDLLInstance, 0);
            //if (KbLLProc == 0)
            //{
            //    int errorCode = Marshal.GetLastWin32Error();
            //    WinAPIs.UnhookWindowsHookEx(KbLLProc);
            //    throw new Win32Exception(errorCode);
            //}
            //MProc = WinAPIs.SetWindowsHookEx(WindowsHookConstans.WH_MOUSE, MouseHookProcedure, RunningDLLInstance, 0);
            //if (MProc == 0)
            //{
            //    int errorCode = Marshal.GetLastWin32Error();
            //    WinAPIs.UnhookWindowsHookEx(MProc);
            //    throw new Win32Exception(errorCode);
            //}
            MLLProc = WinAPIs.SetWindowsHookEx(Constants.WindowsHookConstans.WH_MOUSE_LL, MouseLLHookProcedure, RunningDLLInstance, 0);
            //MLLProc1 = (uint)WinAPIs.SetWindowsHookExNative(MouseHookProcedure, (uint)Constants.WindowsHookConstans.WH_MOUSE_LL, WinAPIs.GetCurrentThreadId());
            if (MLLProc == 0)
            {
                int errorCode = Marshal.GetLastWin32Error();
                WinAPIs.UnhookWindowsHookEx(MLLProc);
                throw new Win32Exception(errorCode);
            }
        }
Esempio n. 11
0
        public string GetUiXPath(int x, int y)
        {
            StringBuilder sb = new StringBuilder(4096, Int16.MaxValue);

            try
            {
                WinAPIs.GetUiXPath(x, y, sb, sb.Capacity);
            }
            catch (Exception e)
            {
                throw;
            }
            return(sb.ToString());
        }
Esempio n. 12
0
        public static void OnApplicationExit(object sender, EventArgs e)
        {
            //c.ShutDownClient();
            WinAPIs.UnInitUiTreeWalk();
            Hooks.UnInstallHooks();
            //Hooks.UnInstallHooks();
            //foreach (var kp in Hooks.UnInstallResults)
            //{
            //    Console.WriteLine("Procedure is : " + kp.Key + " result is : " + kp.Value);
            //}

            WindowEventHooks.UnInstallWindowEventHooks();
            foreach (var kp in Hooks.UnInstallResults)
            {
                Console.WriteLine("Procedure is : " + kp.Key + " result is : " + kp.Value);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// this will set position of the rectangle when location has been changed
        /// </summary>
        private void Layout()
        {
//            SafeNativeMethods.SetWindowPos(this._leftForm.Handle, NativeMethods.HWND_TOPMOST, this._location.Left - this._width, this._location.Top, this._width, this._location.Height, 0x10);
            WinAPIs.SetWindowPos(this._form.Handle, Constants.NativeMethods.HWND_TOPMOST, this._location.X, this._location.Y, this._location.Width, this._location.Height, 0x10);
        }
Esempio n. 14
0
        public static int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr LPARAM)
        {
            ProcStructs.MouseLLHookStruct mouseLLHookStruct = (ProcStructs.MouseLLHookStruct)Marshal.PtrToStructure(LPARAM, typeof(ProcStructs.MouseLLHookStruct));
            MouseButtons button  = MouseButtons.None;
            int          callRes = (int)WinAPIs.CallNextHookEx(RunningDLLInstance, nCode, wParam, LPARAM);

            #region junk
            //// var clickedWinText = new WinAPIs().GetWindowText(Program.targetWinHandle);
            // var clickedWinHandle = WinAPIs.WindowFromPoint(mouseLLHookStruct.pt);
            // //if (!clickedWinText.ToLower().Contains(Program.targetAppName.ToLower()))
            // //    return callRes;
            // if (Program.targetWinHandle!=clickedWinHandle)
            //     return callRes;

            // Console.WriteLine("Api response is :" +(int)clickedWinHandle + "    Targetapplication is :" + (int)Program.targetWinHandle);
            #endregion
            switch (wParam.ToInt32())
            {
            case Constants.MouseEvents.WM_MOUSEMOVE:
                button = MouseButtons.None;

                MouseEventArgs e2 = new MouseEventArgs(
                    button,
                    1,
                    mouseLLHookStruct.pt.X,
                    mouseLLHookStruct.pt.Y,
                    0);
                OnMouseMovement(null, e2);
                return(callRes);

            //case WM_MOUSEWHEEL:
            //    short zDelta = (short)(mhs.mouseData >> 16);
            //    MouseKeyboardEventHandler.MouseWheel(left, top, zDelta);
            //    break;

            //case WM_LBUTTONDOWN:
            //    MouseKeyboardEventHandler.LeftMouseDown(left, top);
            //    break;

            //case WM_LBUTTONUP:
            //    MouseKeyboardEventHandler.LeftMouseUp(left, top);
            //    break;
            case Constants.MouseEvents.WM_RBUTTONDOWN:
                button = MouseButtons.Right;
                MouseEventArgs e = new MouseEventArgs(
                    button,
                    1,
                    mouseLLHookStruct.pt.X,
                    mouseLLHookStruct.pt.Y,
                    0);
                OnMouseActivity(null, e);
                return(-1);

            case Constants.MouseEvents.WM_LBUTTONDOWN:
                button = MouseButtons.Left;
                MouseEventArgs e1 = new MouseEventArgs(
                    button,
                    1,
                    mouseLLHookStruct.pt.X,
                    mouseLLHookStruct.pt.Y,
                    0);

                OnMouseActivity(null, e1);
                return(-1);

            case Constants.MouseEvents.WM_RBUTTONUP:

                return(-1);

            case Constants.MouseEvents.WM_LBUTTONUP:

                return(-1);
            }
            return(callRes);
            //return (int)WinAPIs.CallNextHookEx(RunningDLLInstance, nCode, wParam, LPARAM);
        }
Esempio n. 15
0
        public static int MouseProc(int nCode, IntPtr wParam, IntPtr LPARAM)
        {
            Console.WriteLine("MouseProc Invoked");

            return((int)WinAPIs.CallNextHookEx(RunningDLLInstance, nCode, wParam, LPARAM));
        }