コード例 #1
0
ファイル: FormHelper.cs プロジェクト: robguyoncourt/APE
        private bool EnumWindowsToGetToolTips(IntPtr hWnd, IntPtr lParam)
        {
            uint pid;

            NM.GetWindowThreadProcessId(hWnd, out pid);

            uint currentProcessId = (uint)AUTProcess.Id;

            if (pid == currentProcessId)
            {
                if (NM.IsWindowVisible(hWnd))
                {
                    NM.tagRect WindowSize;
                    NM.GetClientRect(hWnd, out WindowSize);
                    if (WindowSize.right > 0)   //If the window has 0 width then ignore it
                    {
                        string className = NM.GetClassName(hWnd);
                        if (className.Contains("tooltips_class"))
                        {
                            m_ToolTipWindows.Add(hWnd);
                        }
                    }
                }
            }
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Gets the parameters from the message then adds a mouse hook to the specified thread
        /// </summary>
        /// <param name="ptrMessage">A pointer to the message</param>
        /// <param name="messageNumber">The message number</param>
        private unsafe void AddMouseHook(Message *ptrMessage, int messageNumber)
        {
            if (messageNumber != 1)
            {
                throw new Exception("AddMouseHook must be the first message");
            }

            IntPtr handle = GetParameterIntPtr(ptrMessage, 0);

            CleanUpMessage(ptrMessage);

            int threadId = NM.GetWindowThreadProcessId(handle, IntPtr.Zero);

            m_HookWindow = handle;

            // Add the mouse hook
            DebugLogging.WriteLog("Adding Mouse hook");
            m_hMouseHook = NM.SetWindowsHookEx(NM.WH_MOUSE, MouseHookProcedure, IntPtr.Zero, threadId);
            if (m_hMouseHook == 0)
            {
                throw new Exception("SetWindowsHookEx failed to add mouse hook");
            }
            DebugLogging.WriteLog("Added Mouse hook");

            ClearMouseState();
        }
コード例 #3
0
ファイル: APE.Spy.cs プロジェクト: johnsewell/APE
        private bool EnumProc(IntPtr hWnd, IntPtr lParam)
        {
            uint Pid;

            NM.GetWindowThreadProcessId(hWnd, out Pid);

            if (new IntPtr(Pid) == lParam)
            {
                if (NM.IsWindowVisible(hWnd))
                {
                    NM.tagRect WindowSize;
                    NM.GetClientRect(hWnd, out WindowSize);

                    if (WindowSize.right > 0)   //If the window has 0 width then ignore it
                    {
                        TreeNode ParentNode;
                        GetIdentity(IntPtr.Zero, hWnd);
                        string APEType = GetAPEType(m_Identity);
                        ListOfTopLevelWindows.Add(hWnd, m_Identity.Name);
                        if (m_Identity.TechnologyType == "Windows Native")
                        {
                            ParentNode = WindowTree.Nodes.Add("0:" + hWnd.ToString(), APEType + " (Windows Native) [" + hWnd.ToString() + "]");
                        }
                        else
                        {
                            ParentNode = WindowTree.Nodes.Add("0:" + hWnd.ToString(), APEType + " " + m_Identity.Name + " [" + hWnd.ToString() + "]");
                        }
                        AddChildNode(hWnd, ParentNode, hWnd);
                    }
                }
            }
            return(true);
        }
コード例 #4
0
ファイル: APE.Spy.cs プロジェクト: johnsewell/APE
        private bool EnumProcToGetProcesses(IntPtr hWnd, IntPtr lParam)
        {
            uint Pid;

            NM.GetWindowThreadProcessId(hWnd, out Pid);

            if (NM.IsWindowVisible(hWnd))
            {
                NM.tagRect WindowSize;
                NM.GetClientRect(hWnd, out WindowSize);

                if (WindowSize.right > 0)   //If the window has 0 width then ignore it
                {
                    if (!m_Pid.ContainsKey(Pid))
                    {
                        m_Pid.Add(Pid, Pid);
                    }
                }
            }
            return(true);
        }
コード例 #5
0
        public static bool WaitForInputIdle(IntPtr handle, uint timeoutMs)
        {
            int processId;
            int threadId = NM.GetWindowThreadProcessId(handle, out processId);

            if (threadId == 0)
            {
                throw new Exception("Failed to get thread for window");
            }

            if (processId != WaitForInputIdleProcessId)
            {
                WaitForInputIdleProcessId = processId;
                WaitForInputIdleProcess   = Process.GetProcessById(processId);
            }
            else
            {
                WaitForInputIdleProcess.Refresh();
            }

            Stopwatch timer = Stopwatch.StartNew();
            int       x     = 0;

            while (true)
            {
                ProcessThreadCollection threadCollection = WaitForInputIdleProcess.Threads;

                for (int i = 0; i < threadCollection.Count; i++)
                {
                    if (threadCollection[i].Id == threadId)
                    {
                        if (threadCollection[i].ThreadState == System.Diagnostics.ThreadState.Wait)
                        {
                            if (threadCollection[i].WaitReason == ThreadWaitReason.UserRequest)
                            {
                                x++;
                                if (x == 2) //Wait for it to match twice in a row as sometimes the process will go idle then immediately go not idle
                                {
                                    return(true);
                                }
                            }
                            else
                            {
                                x = 0;
                            }
                        }
                        else
                        {
                            x = 0;
                        }
                        break;
                    }
                }

                if (timer.ElapsedMilliseconds > timeoutMs)
                {
                    return(false);
                }

                Thread.Sleep(0);
                WaitForInputIdleProcess.Refresh();
            }
        }
コード例 #6
0
ファイル: FormHelper.cs プロジェクト: robguyoncourt/APE
        private unsafe void PeakMessage(Message *ptrMessage, int messageNumber)
        {
            //must be first message
            if (messageNumber != 1)
            {
                throw new Exception("PeakMessage must be the first message");
            }

            // p1  = handle
            IntPtr handle    = GetParameterIntPtr(ptrMessage, 0);
            int    timeoutMS = GetParameterInt32(ptrMessage, 1);

            int threadId = NM.GetWindowThreadProcessId(handle, out int pid);

            m_AllControls = new List <IntPtr>();
            NM.EnumThreadWindows((uint)threadId, EnumThreadProcedue, IntPtr.Zero);

            Stopwatch timer = Stopwatch.StartNew();

            for (int loop = 0; loop < 2; loop++)
            {
                foreach (IntPtr hWnd in m_AllControls)
                {
                    WF.Control control = WF.Control.FromHandle(hWnd);

                    if (control == null)
                    {
                        //Todo
                    }
                    else
                    {
                        while (true)
                        {
                            bool messageAvailble = false;
                            if (control.IsDisposed)
                            {
                                // Nothing
                            }
                            else if (control.Disposing)
                            {
                                messageAvailble = true; // Don't invoke anything just continue to loop till the control is fully disposed
                            }
                            else if (!control.IsHandleCreated)
                            {
                                // Nothing as to get to here the handle must have existed at some point so it must have been destroyed
                            }
                            else if (control.RecreatingHandle)
                            {
                                messageAvailble = true; // Don't invoke anything just continue to loop till the control has recreated the handle
                            }
                            else if (!control.Enabled)
                            {
                                // Nothing move on to the next window
                            }
                            else
                            {
                                try
                                {
                                    //control.BeginInvoke(m_RefreshControlDelegater, new object[] { control });
                                    IAsyncResult result = control.BeginInvoke(m_PeakMessagDelegater, null);
                                    while (true)
                                    {
                                        int innerLoop = 0;
                                        if (result.IsCompleted)
                                        {
                                            messageAvailble = (bool)control.EndInvoke(result);
                                            break;
                                        }

                                        if (control.IsDisposed)
                                        {
                                            // Nothing
                                            break;
                                        }
                                        else if (control.Disposing)
                                        {
                                            // Don't do anything just continue to loop till the control is fully disposed
                                        }
                                        else if (!control.IsHandleCreated)
                                        {
                                            // Nothing as to get to here the handle must have existed at some point so it must have been destroyed
                                            break;
                                        }
                                        else if (control.RecreatingHandle)
                                        {
                                            messageAvailble = true; // Don't invoke anything just continue to loop till the control has recreated the handle
                                            break;
                                        }
                                        else if (!control.Enabled)
                                        {
                                            // Nothing move on to the next window
                                            break;
                                        }

                                        if (timer.ElapsedMilliseconds > timeoutMS)
                                        {
                                            throw new Exception("Thread failed to have zero messages within timeout");
                                        }

                                        innerLoop++;

                                        if (innerLoop == 100)
                                        {
                                            innerLoop = 0;
                                            Thread.Sleep(15);
                                        }
                                        else
                                        {
                                            Thread.Yield();
                                        }
                                    }
                                }
                                catch (ObjectDisposedException) { }
                                catch (InvalidAsynchronousStateException) { }
                                catch (NullReferenceException) { }
                                catch (InvalidOperationException) { }
                            }

                            if (!messageAvailble)
                            {
                                break;
                            }

                            if (timer.ElapsedMilliseconds > timeoutMS)
                            {
                                throw new Exception("Thread failed to have zero messages within timeout");
                            }

                            Thread.Sleep(15);
                        }
                        break;
                    }
                }
            }

            CleanUpMessage(ptrMessage);
        }
コード例 #7
0
ファイル: APE.Spy.cs プロジェクト: johnsewell/APE
        private void IdentifyButton_Click(object sender, EventArgs e)
        {
            m_CurrentAttached = (KeyValuePair <Process, string>)WinformsProcessesCombobox.SelectedItem;

            if (m_CurrentAttached.Key.HasExited)
            {
                WinformsProcessesCombobox.SelectedIndex = 0;
                Populate();
                return;
            }

            IdentifyButton.Enabled            = false;
            WinformsProcessesCombobox.Enabled = false;
            label1.Text = "Move the mouse cursor over the desired window and then press the control key";

            //install hotkey hook for control key
            if (!NM.RegisterHotKey(this.Handle, 1, NM.MOD_CONTROL, NM.VK_CONTROL))
            {
                throw new Exception("Failed to register hotkey");
            }
            m_ControlKey = false;

            IntPtr OldHandle = IntPtr.Zero;

            while (m_ControlKey == false)
            {
                Point       cursorPosition = Cursor.Position;
                NM.tagPoint screenLocation;
                NM.tagPoint Location;
                IntPtr      Handle;
                IntPtr      ChildWindow;
                IntPtr      parent = NM.GetDesktopWindow();
                screenLocation.x = cursorPosition.X;
                screenLocation.y = cursorPosition.Y;

                while (true)
                {
                    Location = screenLocation;
                    NM.ScreenToClient(parent, ref Location);
                    ChildWindow = NM.RealChildWindowFromPoint(parent, Location);

                    if (ChildWindow == IntPtr.Zero || parent == ChildWindow)
                    {
                        Handle = parent;
                        break;
                    }

                    parent = ChildWindow;
                }

                uint Pid;
                NM.GetWindowThreadProcessId(Handle, out Pid);

                if (Handle != OldHandle)
                {
                    if (OldHandle != IntPtr.Zero)
                    {
                        ClearHighlight(m_Area);
                    }

                    if (m_CurrentAttached.Key.Id == Pid)
                    {
                        PopulatePropertyListbox(NM.GetAncestor(Handle, NM.GetAncestorFlags.GetRoot), Handle);

                        Highlight(Handle);

                        OldHandle = Handle;
                    }
                    else
                    {
                        OldHandle = IntPtr.Zero;
                    }
                }

                Application.DoEvents();
                Thread.Sleep(100);
            }

            if (OldHandle != IntPtr.Zero)
            {
                ClearHighlight(m_Area);

                TreeNode[] Nodes = WindowTree.Nodes.Find(m_Identity.ParentHandle.ToString() + ":" + m_Identity.Handle.ToString(), true);
                if (Nodes.GetLength(0) == 0)
                {
                    BuildTree();
                    Nodes = WindowTree.Nodes.Find(m_Identity.ParentHandle.ToString() + ":" + m_Identity.Handle.ToString(), true);
                }
                if (Nodes.GetLength(0) > 0)
                {
                    WindowTree.SelectedNode = Nodes[0];
                    WindowTree.Focus();
                }
            }

            if (!m_Closing)
            {
                NM.UnregisterHotKey(this.Handle, 1);
                label1.Text                       = "";
                IdentifyButton.Enabled            = true;
                WinformsProcessesCombobox.Enabled = true;
            }
        }