Process with Username in 2.0 pops up window even if CreateNoWindow

  • Thread starter William Stacey [MVP]
  • Start date
W

William Stacey [MVP]

Is this a bug in fx2? I expect they did not carry forward all the StartInfo
properties to api used with Username/Password. When you remove
UserName/password, the window does not show as expected.

private void button5_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir c:\windows\system32";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;

// *Note:
// With UserName/Password, Console window shows when should be hidden.
// Without UserName/Password, Consoel windows is hidden as expected.
p.StartInfo.UserName = "staceyw";
SecureString ss = new SecureString();
string pw = "password"; // Users password.
foreach ( char c in pw )
{
ss.AppendChar(c);
}
p.StartInfo.Password = ss;

p.Start();
StreamReader myStreamReader = p.StandardOutput;
string myString = myStreamReader.ReadToEnd();
Console.WriteLine(myString);
p.Close();
}
 
W

Willy Denoyette [MVP]

William Stacey said:
Is this a bug in fx2? I expect they did not carry forward all the
StartInfo properties to api used with Username/Password. When you remove
UserName/password, the window does not show as expected.

private void button5_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir c:\windows\system32";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;

// *Note:
// With UserName/Password, Console window shows when should be hidden.
// Without UserName/Password, Consoel windows is hidden as expected.
p.StartInfo.UserName = "staceyw";
SecureString ss = new SecureString();
string pw = "password"; // Users password.
foreach ( char c in pw )
{
ss.AppendChar(c);
}
p.StartInfo.Password = ss;

p.Start();
StreamReader myStreamReader = p.StandardOutput;
string myString = myStreamReader.ReadToEnd();
Console.WriteLine(myString);
p.Close();
}

IMO not a bug in fx2, but the result of a CreateProcessWithLogonW bug on XP
pre SP2 and W2K3 pre SP1.

http://support.microsoft.com/?kbid=818858

Willy.
 
W

William Stacey [MVP]

Thanks Willy. That sounds exactly what the issue is, however I have been
use xp w/ sp2. Maybe you (or others) could try the code on their SP2/fx 2.0
build and see if they see the same thing. Thanks much!
 
W

Willy Denoyette [MVP]

William,

What the KB article is talking about is the startinfo.wShowWindow flag or
the .NET equivalent
p.StartInfo.WindowStyle = ProcessWindowStyle.xxxxx;
So you have to set ProcessWindowStyle.Hidden if you want to hide the dos
command window, unfortunately this flag is ignored in Whidbey Beta2.

I tested this with a C++ program that calls CreateProcessWithLogonW API
(this is the API called by .NET when StartInfo.UserName is not empty), and
it works when setting following flags in the STARTUPINFO struct.

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

note that SW_HIDE is ignored (and the window shown) when
STARTF_USESHOWWINDOW is not set, which is the 'documented' behavior (see the
CreateProcessWithLogonW API description).
..NET however fails to set the STARTF_USESHOWWINDOW flag, so it's normal that
ProcessWindowStyle.Hidden is ignored as well.
Now the funny thing is that calling CreateProcess (that is when you don't
specify a StartInfo.UserName)
works as expected while it's using the same STARTUPINFO struct.
So it looks like this is a Beta2 issue, or the documented behavior of
CreateProcessWithLogonW (see STARTF_USESHOWWINDOW) is wrong.

I would suggest you to file a bug.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top