Inheritance: IDisposable
Exemple #1
0
        public int StartProcessUnderDebuggerAndDetach(IList <string> procargs)
        {
            var pi = new WinProcesses.PROCESS_INFORMATION();
            var si = new WinProcesses.STARTUPINFO();
            var processCreationFlags = WinProcesses.ProcessCreationFlags.CREATE_UNICODE_ENVIRONMENT | WinProcesses.ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS;

            if (spawnNewConsoleWindow)
            {
                processCreationFlags |= WinProcesses.ProcessCreationFlags.CREATE_NEW_CONSOLE;
            }

            CheckResult(WinProcesses.NativeMethods.CreateProcess(null, new StringBuilder(string.Join(" ", procargs)), null, null, false,
                                                                 processCreationFlags, GetEnvironmentString(), null, si, pi));

            hProcess = new WinProcesses.SafeProcessHandle(pi.hProcess);
            CheckResult(WinDebug.NativeMethods.DebugSetProcessKillOnExit(false));

            AssignProcessToJobObject();

            // resume process main thread by detaching from the debuggee
            CheckResult(WinDebug.NativeMethods.DebugActiveProcessStop(pi.dwProcessId));
            // and we can close the thread handle
            CloseHandle(pi.hThread);

            return(WaitForTheJobToComplete());
        }
Exemple #2
0
        public int StartProcess(IList <string> procargs)
        {
            var pi = new WinProcesses.PROCESS_INFORMATION();
            var si = new WinProcesses.STARTUPINFO();
            var processCreationFlags = WinProcesses.ProcessCreationFlags.CREATE_UNICODE_ENVIRONMENT | WinProcesses.ProcessCreationFlags.CREATE_SUSPENDED;

            if (spawnNewConsoleWindow)
            {
                processCreationFlags |= WinProcesses.ProcessCreationFlags.CREATE_NEW_CONSOLE;
            }

            CheckResult(WinProcesses.NativeMethods.CreateProcess(null, new StringBuilder(string.Join(" ", procargs)), null, null, false,
                                                                 processCreationFlags, GetEnvironmentString(), null, si, pi));

            hProcess = new WinProcesses.SafeProcessHandle(pi.hProcess);

            AssignProcessToJobObject();

            // resume process main thread
            CheckResult(WinProcesses.NativeMethods.ResumeThread(pi.hThread));
            // and we can close the thread handle
            CloseHandle(pi.hThread);

            return(WaitForTheJobToComplete());
        }
Exemple #3
0
    private ProcessInformation CreateProcessWithStartInfo(SimpleProcessStartupInfo simpleProcessStartupInfo, CreateProcessOptions options) {
      Logger.LogInfo("CreateProcessWithStartInfo: Entry point.");
      var stringBuilder = BuildCommandLine(simpleProcessStartupInfo.FileName, simpleProcessStartupInfo.Arguments);
      Logger.LogInfo("CreateProcessWithStartInfo: command line is {0}.", stringBuilder);

      using (var startupInfo = new STARTUPINFO()) {
        Logger.LogInfo("CreateProcessWithStartInfo: Creation flags.");
        ProcessCreationFlags processCreationFlags = 0;
        if (simpleProcessStartupInfo.CreateNoWindow) {
          processCreationFlags |= ProcessCreationFlags.CREATE_NO_WINDOW;
        }
        if ((options & CreateProcessOptions.BreakAwayFromJob) != 0) {
          processCreationFlags |= ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB;
        }

        var workingDirectory = simpleProcessStartupInfo.WorkingDirectory;
        if (workingDirectory == string.Empty) {
          workingDirectory = Environment.CurrentDirectory;
        }
        Logger.LogInfo("CreateProcessWithStartInfo: Working directory: {0}.", workingDirectory);

        if ((options & CreateProcessOptions.AttachDebugger) != 0) {
          Logger.LogInfo("CreateProcessWithStartInfo: Setting DEBUG_PROCESS flag.");
          processCreationFlags |= ProcessCreationFlags.DEBUG_PROCESS;
          processCreationFlags |= ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS;
        }

        Logger.LogInfo("CreateProcessWithStartInfo: Calling Win32 CreateProcess.");
        var processInformation = new PROCESS_INFORMATION();
        var environmentPtr = IntPtr.Zero;
        var lastError = 0;
        var success = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, processCreationFlags,
                                                 environmentPtr, workingDirectory, startupInfo, processInformation);
        Logger.LogInfo("CreateProcessWithStartInfo: CreateProcess result: Success={0}-LastError={1}.", success, Marshal.GetLastWin32Error());
        if (!success) {
          lastError = Marshal.GetLastWin32Error();
        }
        // Assign safe handles as quickly as possible to avoid leaks.
        var safeProcessHandle = new SafeProcessHandle(processInformation.hProcess);
        var safeThreadHandle = new SafeProcessHandle(processInformation.hThread);
        if (!success) {
          throw new LastWin32ErrorException(lastError, string.Format("Error creating process from file \"{0}\"", simpleProcessStartupInfo.FileName));
        }

        if (safeProcessHandle.IsInvalid || safeThreadHandle.IsInvalid) {
          Logger.LogInfo("CreateProcessWithStartInfo: Invalid process handle.");
          throw new Exception(string.Format("Error creating process from file \"{0}\" (invalid process handle)", simpleProcessStartupInfo.FileName));
        }

        Logger.LogInfo("CreateProcessWithStartInfo: Creating ProcessResult instance.");
        var processResult = new ProcessInformation {
          ProcessHandle = safeProcessHandle,
          ProcessId = processInformation.dwProcessId
        };
        safeThreadHandle.Close();

        Logger.LogInfo("CreateProcessWithStartInfo: Success!");
        return processResult;
      }
    }
Exemple #4
0
   public static extern bool CreateProcess(
 [MarshalAs(UnmanagedType.LPTStr)] string lpApplicationName,
 StringBuilder lpCommandLine,
 SecurityAttributes lpProcessAttributes,
 SecurityAttributes lpThreadAttributes,
 bool bInheritHandles,
 ProcessCreationFlags dwCreationFlags,
 IntPtr lpEnvironment,
 [MarshalAs(UnmanagedType.LPTStr)] string lpCurrentDirectory,
 STARTUPINFO lpStartupInfo,
 [In, Out]
 PROCESS_INFORMATION lpProcessInformation);
Exemple #5
0
        public void StartSuspended()
        {
            var pi = new WinProcesses.PROCESS_INFORMATION();
            var si = new WinProcesses.STARTUPINFO();
            var processCreationFlags = WinProcesses.ProcessCreationFlags.CREATE_SUSPENDED;

            if (SpawnNewConsoleWindow)
            {
                processCreationFlags |= WinProcesses.ProcessCreationFlags.CREATE_NEW_CONSOLE;
            }

            if (!WinProcesses.NativeMethods.CreateProcess(null, new StringBuilder(string.Join(" ", args)), null, null, false,
                                                          processCreationFlags, null, null, si, pi))
            {
                throw new Win32Exception("Error while creating a new process.");
            }

            hProcess = new WinProcesses.SafeProcessHandle(pi.hProcess);
            pid      = pi.dwProcessId;
            hThread  = new WinProcesses.SafeThreadHandle(pi.hThread);
        }
Exemple #6
0
        void CreateProcess()
        {
            bool spawnNew = !string.IsNullOrEmpty(_options.DumpFolderForNewlyStartedProcess);
            _processName = null;

            int pid;
            if (int.TryParse(_options.ProcessInfo, out pid)) {
                _pid = pid;
            } else {
                // not numeric - let's try to find it by name
                var procs = Process.GetProcesses();
                foreach (var proc in procs) {
                    try {
                        if (_options.ProcessInfo.Equals(proc.MainModule.ModuleName, StringComparison.OrdinalIgnoreCase)) {
                            _pid = proc.Id;
                            break;
                        }
                    } catch {
                        // just ignore it
                    }
                }
            }
            if (_pid > 0) {
                // process found - let's attach to it
                if (!DebuggingNativeMethods.DebugActiveProcess(_pid)) {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                _processName = GetProcessName(_pid);
                return;
            }
            if (spawnNew) {
                // final try - let's try creating it (but only if -x option is set)
                var commandLine = _options.ProcessInfo + " " + string.Join(" ", _options.Args ?? new string[0]);

                var startupInfo = new STARTUPINFO();
                var processInformation = new PROCESS_INFORMATION();
                var processCreationFlags = ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS;
                if (_options.StartProcessInNewConsoleWindow) {
                    processCreationFlags |= ProcessCreationFlags.CREATE_NEW_CONSOLE;
                }
                bool res = ProcessNativeMethods.CreateProcess(null, new StringBuilder(commandLine),
                    null, null, false, processCreationFlags, IntPtr.Zero, null,
                    startupInfo, processInformation);
                if (!res) {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                if (!DebuggingNativeMethods.DebugSetProcessKillOnExit(false)) {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                _pid = processInformation.dwProcessId;
                _processName = GetProcessName(_pid);
                return;
            }
            throw new ArgumentException("Something is wrong with the arguments - couldn't find or create a requested process.");
        }