/// <summary> /// Create process with a token. /// </summary> /// <param name="token">The token to create the process with.</param> /// <param name="config">The process configuration.</param> /// <returns>The created win32 process.</returns> public static Win32Process CreateProcessAsUser(NtToken token, Win32ProcessConfig config) { using (var resources = new DisposableList <IDisposable>()) { PROCESS_INFORMATION proc_info = new PROCESS_INFORMATION(); STARTUPINFOEX start_info = config.ToStartupInfoEx(resources); SECURITY_ATTRIBUTES proc_attr = config.ProcessSecurityAttributes(resources); SECURITY_ATTRIBUTES thread_attr = config.ThreadSecurityAttributes(resources); if (!CreateProcessAsUser(token.Handle, config.ApplicationName, config.CommandLine, proc_attr, thread_attr, config.InheritHandles, config.CreationFlags | CreateProcessFlags.ExtendedStartupInfoPresent, config.Environment, config.CurrentDirectory, start_info, out proc_info)) { if (!CreateProcessWithTokenW(token.Handle, 0, config.ApplicationName, config.CommandLine, config.CreationFlags, config.Environment, config.CurrentDirectory, ref start_info.StartupInfo, out proc_info)) { throw new SafeWin32Exception(); } } return(new Win32Process(proc_info, config.TerminateOnDispose)); } }
/// <summary> /// Create process. /// </summary> /// <param name="config">The process configuration.</param> /// <returns>The created win32 process.</returns> public static Win32Process CreateProcess(Win32ProcessConfig config) { if (config.Token != null) { return(CreateProcessAsUser(config.Token, config)); } PROCESS_INFORMATION proc_info = new PROCESS_INFORMATION(); using (var resources = new DisposableList <IDisposable>()) { SECURITY_ATTRIBUTES proc_attr = config.ProcessSecurityAttributes(resources); SECURITY_ATTRIBUTES thread_attr = config.ThreadSecurityAttributes(resources); using (var debug_object = config.SetDebugObject()) { if (!Win32NativeMethods.CreateProcess(config.ApplicationName, config.CommandLine, proc_attr, thread_attr, config.InheritHandles, config.CreationFlags | CreateProcessFlags.ExtendedStartupInfoPresent, config.Environment, config.CurrentDirectory, config.ToStartupInfoEx(resources), out proc_info)) { throw new SafeWin32Exception(); } } return(new Win32Process(proc_info, config.TerminateOnDispose)); } }