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

            PopulateStartupInfo(ref start_info.StartupInfo);
            start_info.lpAttributeList = GetAttributes(resources);
            return(start_info);
        }
 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);
Esempio n. 4
0
        public static Win32Process CreateProcess(NtProcess parent, string application_name, string command_line, CreateProcessFlags flags, string desktop)
        {
            STARTUPINFOEX start_info = new STARTUPINFOEX();

            start_info.StartupInfo.lpDesktop = desktop;
            PROCESS_INFORMATION proc_info = new PROCESS_INFORMATION();

            using (SafeProcThreadAttributeListBuffer attr_list = new SafeProcThreadAttributeListBuffer(1))
            {
                using (var handle_buffer = parent.Handle.DangerousGetHandle().ToBuffer())
                {
                    attr_list.AddAttribute(new IntPtr(0x00020000), handle_buffer);
                    start_info.lpAttributeList = attr_list.DangerousGetHandle();

                    if (!CreateProcess(application_name, command_line, IntPtr.Zero, IntPtr.Zero, false,
                                       flags | CreateProcessFlags.EXTENDED_STARTUPINFO_PRESENT, IntPtr.Zero, null, start_info, out proc_info))
                    {
                        throw new SafeWin32Exception();
                    }

                    return(new Win32Process(proc_info));
                }
            }
        }