/// <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);

                using (var debug_object = config.SetDebugObject())
                {
                    if (Win32NativeMethods.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))
                    {
                        return(new Win32Process(proc_info, config.TerminateOnDispose));
                    }
                }

                if (config.NoTokenFallback)
                {
                    throw new SafeWin32Exception();
                }

                if (!Win32NativeMethods.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));
            }
        }
Beispiel #2
0
        /// <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));
            }
        }