Esempio n. 1
0
        /// <summary>
        /// Tries to kill the process and all child processes.
        /// </summary>
        /// <remarks>
        /// It's okay to call this method at any time; however, before process start and after process termination or disposing of
        /// this instance, it does nothing.
        /// </remarks>
        public void Kill(int exitCode)
        {
            // Notify the injected that the process is being killed
            m_processInjector?.OnKilled();

            var processHandle = m_processHandle;

            if (processHandle != null && !processHandle.IsInvalid)
            {
                // Ignore result, as there is a race with regular process termination that we cannot do anything about.
                m_killed = true;

                // No job object means that we are on an old OS; let's just terminate this process (we can't reliably terminate all child processes)
                Analysis.IgnoreResult(Native.Processes.ProcessUtilities.TerminateProcess(processHandle, exitCode));
            }

            JobObject jobObject = m_job;

            if (jobObject != null)
            {
                // Ignore result, as there is a race with regular shutdown.
                m_killed = true;

                Analysis.IgnoreResult(jobObject.Terminate(exitCode));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to kill the process and all child processes.
        /// </summary>
        /// <remarks>
        /// It's okay to call this method at any time; however, before process start and after process termination or disposing of
        /// this instance, it does nothing.
        /// </remarks>
        public void Kill(int exitCode)
        {
            // Notify the injected that the process is being killed
            m_processInjector?.OnKilled();
            LogDiagnostics($"Process will be killed with exit code {exitCode}");

            var processHandle = m_processHandle;

            if (processHandle != null && !processHandle.IsInvalid)
            {
                // Ignore result, as there is a race with regular process termination that we cannot do anything about.
                m_killed = true;
                // No job object means that we are on an old OS; let's just terminate this process (we can't reliably terminate all child processes)
                Analysis.IgnoreResult(Native.Processes.ProcessUtilities.TerminateProcess(processHandle, exitCode));
                LogDiagnostics("DetouredProcess is killed using TerminateProcess");
            }

            using (m_queryJobDataLock.AcquireWriteLock())
            {
                JobObject jobObject = m_job;
                if (jobObject != null)
                {
                    // Ignore result, as there is a race with regular shutdown.
                    m_killed = true;
                    Analysis.IgnoreResult(jobObject.Terminate(exitCode));

                    LogDiagnostics("DetouredProcess is killed via JobObject termination");
                }
            }

            LogDiagnostics("Stack trace:");
            LogDiagnostics(Environment.StackTrace);
        }