/// <summary> /// Create process with a token from a user logon. /// </summary> /// <param name="credentials">The user's credentials.</param> /// <param name="logon_flags">Logon flags.</param> /// <param name="config">The process configuration.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The created win32 process.</returns> public static NtResult <Win32Process> CreateProcessWithLogon(UserCredentials credentials, CreateProcessLogonFlags logon_flags, Win32ProcessConfig config, bool throw_on_error) { STARTUPINFO start_info = config.ToStartupInfo(); PROCESS_INFORMATION proc_info = new PROCESS_INFORMATION(); using (var password = credentials.GetPassword()) { return(Win32NativeMethods.CreateProcessWithLogonW(credentials.UserName, credentials.Domain, password, logon_flags, config.ApplicationName, config.CommandLine, config.CreationFlags, config.Environment, config.CurrentDirectory, ref start_info, out proc_info).CreateWin32Result(throw_on_error, () => new Win32Process(proc_info, config.TerminateOnDispose))); } }
/// <summary> /// Create process with a token from a user logon. /// </summary> /// <param name="username">The username.</param> /// <param name="domain">The user's domain.</param> /// <param name="password">The user's password.</param> /// <param name="logon_flags">Logon flags.</param> /// <param name="config">The process configuration.</param> /// <returns>The created win32 process.</returns> public static Win32Process CreateProcessWithLogin(string username, string domain, string password, CreateProcessLogonFlags logon_flags, Win32ProcessConfig config) { STARTUPINFO start_info = config.ToStartupInfo(); PROCESS_INFORMATION proc_info = new PROCESS_INFORMATION(); if (!CreateProcessWithLogonW(username, domain, password, logon_flags, config.ApplicationName, config.CommandLine, config.CreationFlags, config.Environment, config.CurrentDirectory, ref start_info, out proc_info)) { throw new SafeWin32Exception(); } return(new Win32Process(proc_info, config.TerminateOnDispose)); }
/// <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 CreateProcessWithToken(NtToken token, Win32ProcessConfig config) { using (var resources = new DisposableList()) { PROCESS_INFORMATION proc_info = new PROCESS_INFORMATION(); STARTUPINFO start_info = config.ToStartupInfo(); SECURITY_ATTRIBUTES proc_attr = config.ProcessSecurityAttributes(resources); SECURITY_ATTRIBUTES thread_attr = config.ThreadSecurityAttributes(resources); if (!Win32NativeMethods.CreateProcessWithTokenW(token.Handle, config.LogonFlags, config.ApplicationName, config.CommandLine, config.CreationFlags, config.Environment, config.CurrentDirectory, ref start_info, out proc_info)) { throw new SafeWin32Exception(); } return(new Win32Process(proc_info, config.TerminateOnDispose)); } }