Re: Process start error when using credentials ("The handle is invalid")

Stuart Dunkeld <[email protected]> Mon, 9 Mar 2009 22:27:56 +0000
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
You need to set all three redirects, RedirectStandardOutput,
RedirectStandardError and RedirectStandardInput:

"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"."

See http://weblogs.asp.net/hernandl/archive/2005/12/02/startprocessasuser.aspx

--stuart

On Mon, Mar 9, 2009 at 10:04 PM, Clark, Michael (OFM)
<[email protected]> wrote:
> I have an Windows Form application that supplies the User Name, Domain,
> and Password to the StartInfo, and it throws this:
>
> System.ComponentModel.Win32Exception: The handle is invalid at
> System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo
> startInfo) at System.Diagnostics.Process.Start()
>
> When I allow the credentials to default to current user I get no such
> error, and the process I start works to the extent that it doesn't need
> to use credentials (the creds are necessary for mapping a drive in an
> MSBuild script). Here's the code that fills the start info:
>
> Process p = new Process();
> ProcessStartInfo si = new ProcessStartInfo("MSBuild.exe",
> "TheMSBProj.proj");
> si.WorkingDirectory = msBuildWorkingDir;
> si.UserName = txtUserName.Text;
> char[] psw = txtPassword.Text.ToCharArray();
> SecureString ss = new SecureString();for (int x = 0; x < psw.Length;
> x++)
> {
>    ss.AppendChar(psw[x]);
> }
> si.Password = ss;
> si.Domain = "ABC";
> si.RedirectStandardOutput = true;
> si.UseShellExecute = false;
> si.WorkingDirectory = txtWorkingDir.Text;
> p.StartInfo = si;
> p.Start();
>
> It isn't that the user/psw isn't matching, because when I provide a bad
> psw, for example, it catches it. So, this "invalid handle" thing is
> happening after the cred is passed. Any ideas on what I might be
> omitting or screwing up?
>
> ===================================
> View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives
>

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives