Beispiel #1
0
        /// <summary>
        /// Finds the pointer to the window containing the infra console.
        /// This is needed when hosted by a parent process, as we won't own that window.
        /// Is disruptive, so we only do it once.
        /// </summary>
        /// <returns></returns>
        static private IntPtr FindConsole()
        {
            // System.Console APIs (e.g. Console.Title) will crash if no console is available for the current process.
            // Attach or allocate a console for this process.
            var processId = Process.GetCurrentProcess().Id;

            if (!AttachConsole((uint)processId))
            {
                int lastError = Marshal.GetLastWin32Error();

                // Process is already attached to another console
                if (lastError == ERROR_ACCESS_DENIED)
                {
                    // Do nothing.  Console is already available for this process.
                }

                else if (lastError == ERROR_INVALID_HANDLE)
                {
                    // Process does not have a console.  Create one.
                    if (!AllocConsole())
                    {
                        throw new Exception($"QualityVault: Microsoft.Test.Execution.EngineCommands.MoveWindowCommand: AllocConsole failed [{Marshal.GetLastWin32Error()}].");
                    }
                }

                else if (lastError == ERROR_INVALID_PARAMETER)
                {
                    // Process id passed in to AttachConsole is invalid.
                    throw new Exception($"QualityVault: Microsoft.Test.Execution.EngineCommands.MoveWindowCommand: ProcessId is invalid [{processId}].");
                }

                else
                {
                    // Unknown error.  AttachConsole failed.
                    throw new Exception($"QualityVault: Microsoft.Test.Execution.EngineCommands.MoveWindowCommand: could not attach or create console [{lastError}].");
                }
            }

            IntPtr window   = (IntPtr)null;
            string previous = Console.Title;
            string target   = "Finding_Infra_Capture_Console";

            Console.Title = target;
            foreach (Process p in Process.GetProcesses())
            {
                try
                {
                    if (!ProcessUtilities.IsCriticalProcess(p) && p.MainWindowTitle == target)
                    {
                        window = p.MainWindowHandle;
                    }
                }
                catch (Exception) { }
            }
            Console.Title = previous;
            return((IntPtr)window);
        }
Beispiel #2
0
        /// <summary>
        /// Determines if a process should not be kept around after test execution
        /// </summary>
        /// <returns>whether this process is unwanted</returns>
        private static bool IsUnwantedProcess(DateTime startTime, string userSid, Process process)
        {
            // Don't shutdown a critical process or the developers IDE
            if (ProcessUtilities.IsCriticalProcess(process) || ProcessUtilities.IsIDE(process))
            {
                return(false);
            }

            // Shutdown any process started within the span of the test. This seems overly aggressive but probably has good reason.
            if (userSid.Equals(ProcessUtilities.GetProcessUserSid(process), StringComparison.InvariantCultureIgnoreCase) && !process.HasExited && process.StartTime > startTime)
            {
                return(true);
            }

            // Shutdown any known processes associated with tests
            if (ProcessUtilities.IsKnownTestProcess(process))
            {
                return(true);
            }

            return(false);
        }