/// <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));
            }
        }
Exemple #2
0
        internal STARTUPINFOEX ToStartupInfoEx(DisposableList <IDisposable> resources)
        {
            STARTUPINFOEX start_info = new STARTUPINFOEX();

            PopulateStartupInfo(ref start_info.StartupInfo);
            start_info.lpAttributeList = GetAttributes(resources);
            return(start_info);
        }
Exemple #3
0
 internal static extern bool CreateProcess(
     string lpApplicationName,
     string lpCommandLine,
     [In] SECURITY_ATTRIBUTES lpProcessAttributes,
     [In] SECURITY_ATTRIBUTES lpThreadAttributes,
     bool bInheritHandles,
     CreateProcessFlags dwCreationFlags,
     [In] byte[] lpEnvironment,
     string lpCurrentDirectory,
     [In] STARTUPINFOEX lpStartupInfo,
     out PROCESS_INFORMATION lpProcessInformation);