Start a new Process as another user

On my last post User Credentials CommandDialog with SecureString password I showed an example on how to get the user credentials using a standard windows dialog and spawn a new process with those credentials.

Now let’s assume that you want to get the output/input/errors of that process from the standard streams (that is an interprocess communication channel) and therefore you set the required redirects property. Let’s say that you want to get the output result from a process that executes a command line tool, so you will do something like this:

 

 

ProcessStartInfo info = new ProcessStartInfo("notepad.exe");

info.UseShellExecute = false;

info.RedirectStandardOutput = true;

 

 

The problem with the above code is that you need to redirect the three streams; otherwise you’ll receive an exception like this:

 

 

System.ComponentModel.Win32Exception: The handle is invalid.

 

 

So the code to start a new process with user credentials and standard stream redirection may look like this:

 

 

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");

info.UseShellExecute = false;

info.RedirectStandardInput = true;

info.RedirectStandardError = true;

info.RedirectStandardOutput = true;

info.UserName = dialog.User; // see the link mentioned at the top

info.Password = dialog.Password;

using (Process install = Process.Start(info))

{

      string output = install.StandardOutput.ReadToEnd();

      install.WaitForExit();

      // Do something with you output data

      Console.WriteLine(output);

}

 

 

Note: The Three-Redirect requirement is valid whenever these two conditions are met:

 

1)    Starting a new process with a different account than the parent process.

2)    Your parent process is running inside a VS add-in or VS Test project.

 

If you want further information about these scenarios, you may find it here and here.

 

This is the technical explanation given in one of these reported bugs.

 

Specifying any one of
RedirectStandardOutput=true,
RedirectStandardError=true, or
RedirectStandardInput=true
causes the process to be launched with STARTF_USESTDHANDLES (See the Win32 API CreateProcessWithLogon). This flag applies to all three handles, whether they are specified or not. If your process does not have any of these handles, then CreateProcessWithLogon will fail with "Invalid Handle".

Console applications have StandardInput connected to the console keyboard by default. GUI processes do not have StandardInput handles by default, so you MUST redirect it (even if you don't intend to write anything to it).

  

This posting is provided "AS IS" with no warranties, and confers no rights. 

Published Friday, December 02, 2005 7:46 AM by HernanDL
Filed under:

Comments

# re: Start a new Process as another user

Tuesday, May 22, 2007 8:48 PM by JackOfPH

Great! Thanks for the information!

Jack

# re: Start a new Process as another user

Thursday, July 26, 2007 6:56 PM by phyu

i want to decmber@gmail.com of password.

# re: Start a new Process as another user

Thursday, July 26, 2007 7:00 PM by phyu

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");

info.UseShellExecute = false;

info.RedirectStandardInput = true;

info.RedirectStandardError = true;

info.RedirectStandardOutput = true;

info.UserName = dialog.User; // see the link mentioned at the top

info.Password = dialog.Password;

using (Process install = Process.Start(info))

{

     string output = install.StandardOutput.ReadToEnd();

     install.WaitForExit();

     // Do something with you output data

     Console.WriteLine(output);

}

# re: Start a new Process as another user

Wednesday, August 08, 2007 8:40 PM by Ashish Raje

Thanks, the tip to redirect all 3 streams was a real life saver.

# re: Start a new Process as another user

Wednesday, February 27, 2008 10:41 AM by linstead

thanks for your help

i forgot to put RedirectStandardInput=true

thanks

# re: Start a new Process as another user

Thursday, May 08, 2008 11:00 AM by Nazer

That was so excellent.

# re: Start a new Process as another user

Friday, July 25, 2008 3:38 AM by liheyuan

thanks a lot

"If your process does not have any of these handles, then CreateProcessWithLogon will fail with "Invalid Handle"."

It help me get out of the troble "invalid handle".(I forget to set the errPipe)

# re: Start a new Process as another user

Tuesday, December 02, 2008 11:44 PM by Starbuck

I was getting "The handle is invalid" error starting up a console routine from a Windows service. I found a bug report to Microsoft saying this message should be changed to something more suitable for diagnostics. Figuring out "what handle?!" was impossible. I found the advice you've provided here in a couple other places but it didn't sink in until I read your posting here a couple times. Thanks for the VERY helpful info.

# re: Start a new Process as another user

Saturday, January 10, 2009 1:57 PM by Christopher G. Lewis

Hi - I'm using your technique to automate a sharepoint install (running the install as an admin, then the PSCONFIG commands as the account w/ rights to the DB) and it works great except for one issue - I can't get the console windows to appear hidden or Minimized - is this an issue with the API wrapper?

Chris

# re: Start a new Process as another user

Tuesday, February 24, 2009 9:30 PM by Art

               pr.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

               pr.StartInfo.CreateNoWindow = true;

# re: Start a new Process as another user

Thursday, July 16, 2009 1:26 PM by propulsivo

Hello.

I'am try to use it in web-service but no luck. The process was not started.

Did you know how can launch winform application from web-service under a specific user?

# re: Start a new Process as another user

Saturday, May 21, 2011 8:45 AM by weblogs.asp.net

Startprocessasuser.. Smashing :)

# Windows “Run As” Without Knowing the Password Drija

Thursday, June 02, 2011 2:13 AM by Windows “Run As” Without Knowing the Password Drija

Pingback from  Windows “Run As” Without Knowing the Password Drija

# re: Start a new Process as another user

Sunday, July 17, 2011 10:57 PM by jsryu21

Thx!!!!!

I want to use "UserName" and "RedirectStandardOutput" at the same time, but Win32Exception welcomed me.

When I corrected "startInfo.RedirectStandardOutput = true;" to

"startInfo.RedirectStandardOutput = true;

startInfo.RedirectStandardInput = true;

startInfo.RedirectStandardError = true;" and it got solved~!

Leave a Comment

(required) 
(required) 
(optional)
(required)