public static extern bool CreateProcess( string lpApplicationName, // LPCTSTR [In] StringBuilder lpCommandLine, // LPTSTR - note: CreateProcess might insert a null somewhere in this string SecurityAttributes lpProcessAttributes, // LPSECURITY_ATTRIBUTES SecurityAttributes lpThreadAttributes, // LPSECURITY_ATTRIBUTES bool bInheritHandles, // BOOL int dwCreationFlags, // DWORD IntPtr lpEnvironment, // LPVOID string lpCurrentDirectory, // LPCTSTR CreateProcessStartupInfo lpStartupInfo, // LPSTARTUPINFO CreateProcessProcessInformation lpProcessInformation // LPPROCESS_INFORMATION );
public void StartAsUser(IntPtr userToken) { if (StartInfo.UseShellExecute) { throw new InvalidOperationException("can't call this with shell execute"); } IntPtr primayUserToken = userToken; var startupInfo = new CreateProcessStartupInfo(); var processInformation = new CreateProcessProcessInformation(); IntPtr stdoutReadHandle = IntPtr.Zero; IntPtr stdoutWriteHandle = IntPtr.Zero; string commandLine = GetCommandLine(); try{ IntPtr stdinHandle = GetStdHandle(StdInputHandle); MyCreatePipe(out stdoutReadHandle, out stdoutWriteHandle, false); IntPtr stderrHandle = GetStdHandle(StdErrorHandle); startupInfo.dwFlags = StartfUsestdhandles; startupInfo.hStdInput = stdinHandle; startupInfo.hStdOutput = stdoutWriteHandle; startupInfo.hStdError = stderrHandle; const int creationFlags = 0; IntPtr environment = IntPtr.Zero; string workingDirectory = GetWorkingDirectory(); if (!CreateProcessAsUserW(primayUserToken, null, commandLine, null, null, true, creationFlags, environment, workingDirectory, startupInfo, processInformation)) { throw new Win32Exception(); } Trace.TraceInformation("-----Process Created-------"); Trace.TraceInformation("cmd=" + commandLine); Trace.TraceInformation("hprocess=" + processInformation.hProcess); var processById = GetProcessById(processInformation.dwProcessId); processById.WaitForExit(); Trace.TraceInformation("Process finished"); } catch (Exception e) { Trace.TraceError(e.ToString()); } finally{ if (processInformation.hThread != _invalidHandleValue) { CloseHandle(new HandleRef(this, processInformation.hThread)); } CloseHandle(new HandleRef(this, stdoutWriteHandle)); } if (processInformation.hProcess == IntPtr.Zero) { throw new Exception("failed to create process"); } Encoding encoding = Encoding.GetEncoding(GetConsoleOutputCP()); var fileStream = new FileStream(new SafeFileHandle(stdoutReadHandle, true), FileAccess.Read, 4096, true); var standardOutput = new StreamReader(fileStream, encoding); Trace.TraceInformation("----------------------------------stdOutput----------------------------------"); Trace.TraceInformation(standardOutput.ReadToEnd()); }
public static extern bool CreateProcessAsUserW(IntPtr token, [MarshalAs(UnmanagedType.LPTStr)] string lpApplicationName, [MarshalAs(UnmanagedType.LPTStr)] string lpCommandLine, SecurityAttributes lpProcessAttributes, SecurityAttributes lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, IntPtr lpEnvironment, [MarshalAs(UnmanagedType.LPTStr)] string lpCurrentDirectory, CreateProcessStartupInfo lpStartupInfo, CreateProcessProcessInformation lpProcessInformation);
public void StartAsUser(IntPtr userToken) { if (StartInfo.UseShellExecute){ throw new InvalidOperationException("can't call this with shell execute"); } IntPtr primayUserToken = userToken; var startupInfo = new CreateProcessStartupInfo(); var processInformation = new CreateProcessProcessInformation(); IntPtr stdoutReadHandle = IntPtr.Zero; IntPtr stdoutWriteHandle = IntPtr.Zero; string commandLine = GetCommandLine(); try{ IntPtr stdinHandle = GetStdHandle(StdInputHandle); MyCreatePipe(out stdoutReadHandle, out stdoutWriteHandle, false); IntPtr stderrHandle = GetStdHandle(StdErrorHandle); startupInfo.dwFlags = StartfUsestdhandles; startupInfo.hStdInput = stdinHandle; startupInfo.hStdOutput = stdoutWriteHandle; startupInfo.hStdError = stderrHandle; const int creationFlags = 0; IntPtr environment = IntPtr.Zero; string workingDirectory = GetWorkingDirectory(); if (!CreateProcessAsUserW(primayUserToken,null,commandLine,null,null,true,creationFlags,environment,workingDirectory,startupInfo,processInformation)){ throw new Win32Exception(); } Trace.TraceInformation("-----Process Created-------"); Trace.TraceInformation("cmd=" + commandLine); Trace.TraceInformation("hprocess=" + processInformation.hProcess); var processById = GetProcessById(processInformation.dwProcessId); processById.WaitForExit(); Trace.TraceInformation("Process finished"); } catch (Exception e){ Trace.TraceError(e.ToString()); } finally{ if (processInformation.hThread != _invalidHandleValue){ CloseHandle(new HandleRef(this, processInformation.hThread)); } CloseHandle(new HandleRef(this, stdoutWriteHandle)); } if (processInformation.hProcess == IntPtr.Zero){ throw new Exception("failed to create process"); } Encoding encoding = Encoding.GetEncoding(GetConsoleOutputCP()); var fileStream = new FileStream(new SafeFileHandle(stdoutReadHandle,true), FileAccess.Read, 4096, true); var standardOutput = new StreamReader(fileStream, encoding); Trace.TraceInformation("----------------------------------stdOutput----------------------------------"); Trace.TraceInformation(standardOutput.ReadToEnd()); }