CloseMainWindow() doesn't appear to work under Vista

G

Guest

I have written a very simple application that simply spawns IE then closes
it. The issue I have is that CloseMainWindow() always returns a false
indicating the message was never sent. Does someone know why?

This application is nothing more than a form with a button. The button's
code below spawns IE.

private void button1_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < 2; i++)
{
System.Diagnostics.Process spawned = new Process();
spawned.StartInfo.FileName = "IEXPLORE.EXE";
spawned.StartInfo.UseShellExecute = true;
spawned.StartInfo.Arguments = "http://WWW.MSN.COM";
spawned.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
try
{
spawned.Start();
if (spawned.Responding)
{
int count = 0;
while (!spawned.HasExited)
{
if (spawned.CloseMainWindow())
Console.WriteLine("Exit message sent:" +
count++);
else
Console.WriteLine("Exit message was not
sent: " + count++);
Thread.Sleep(2000);
}
spawned.WaitForExit();
int exitCode = spawned.ExitCode;
Console.WriteLine("exit code: " + exitCode);
spawned.Close();
}
}
catch (Exception L2)
{
string s = String.Format("{0}\n{1}\nL2", L2.Message,
L2.StackTrace);
Console.WriteLine(s);
}
}
}
catch (Exception L1)
{
string s = String.Format("{0}\n{1}\nL1", L1.Message,
L1.StackTrace);
Console.WriteLine(s);
}
}
 
W

Willy Denoyette [MVP]

SteveT said:
I have written a very simple application that simply spawns IE then closes
it. The issue I have is that CloseMainWindow() always returns a false
indicating the message was never sent. Does someone know why?

This application is nothing more than a form with a button. The button's
code below spawns IE.

private void button1_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < 2; i++)
{
System.Diagnostics.Process spawned = new Process();
spawned.StartInfo.FileName = "IEXPLORE.EXE";
spawned.StartInfo.UseShellExecute = true;
spawned.StartInfo.Arguments = "http://WWW.MSN.COM";
spawned.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
try
{
spawned.Start();
if (spawned.Responding)
{
int count = 0;
while (!spawned.HasExited)
{
if (spawned.CloseMainWindow())
Console.WriteLine("Exit message sent:" +
count++);
else
Console.WriteLine("Exit message was not
sent: " + count++);
Thread.Sleep(2000);
}
spawned.WaitForExit();
int exitCode = spawned.ExitCode;
Console.WriteLine("exit code: " + exitCode);
spawned.Close();
}
}
catch (Exception L2)
{
string s = String.Format("{0}\n{1}\nL2", L2.Message,
L2.StackTrace);
Console.WriteLine(s);
}
}
}
catch (Exception L1)
{
string s = String.Format("{0}\n{1}\nL1", L1.Message,
L1.StackTrace);
Console.WriteLine(s);
}
}


This can only be done from an elevated process on Vista.

Willy.
 
L

Linda Liu [MSFT]

Hi Steve,

I performed a test based on your sample code and did see the problem.

I also run the application on Windows XP and see that the CloseMainWindow
method always returns false. So this problem does not only exist on Vista.

I think the reason of the problem is that you're trying to send 'close main
window' message to the process when the process has not finished
establishing its message loop yet, because you call the CloseMainWindow
method immediately after the process starts.

In addition, we need to elevate the application to send the 'close main
window' message to a process.

I add the line of code 'Thread.Sleep(3000);' after the line of coe
'spawned.Start();'. Run the application as administrator. All works fine
now on my part.

Please try my suggestion and let me know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

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

Guest

Your analysis has been very helpful. I did indeed try your suggestion and
the program did work correct, but only if run as an administrator. Is there
a way programmatically to permit the application to run as an administrator?
 
W

Willy Denoyette [MVP]

SteveT said:
Your analysis has been very helpful. I did indeed try your suggestion and
the program did work correct, but only if run as an administrator. Is there
a way programmatically to permit the application to run as an administrator?

Yes, there is, what you need to do is insert a manifest to the executable file, currently
there is no support in VS2005 for C# to do this, so you'll have to create the manifest file
and add this by running the mt.exe utility.
More details here:
http://download.microsoft.com/downl...-932b-38acd478f46d/WindowsVistaUACDevReqs.doc

However, it's really a bad idea to run as full administrator only because you need to close
a low-integrity process, does your application have other administrative requirements? If
no, then you should run this program with low integrity level, just like IE. Running your
program with low integrity level will allow it to close another low integrity level
application like IE.

Willy.
 
G

Guest

How then do I get my application to run and close IE and other EXE's that I
may spawn? How do I make my application "low-integrity" so as to close a
spawned process without having to specifically tell the EXE to run in admin
mode?
 
W

Willy Denoyette [MVP]

SteveT said:
How then do I get my application to run and close IE and other EXE's that I
may spawn? How do I make my application "low-integrity" so as to close a
spawned process without having to specifically tell the EXE to run in admin
mode?




It's really important to fully understand both UAC and Integrity levels, before starting
development on Vista.
This document
<http://download.microsoft.com/downl...-932b-38acd478f46d/WindowsVistaUACDevReqs.doc>
describes all you need to know about UAC, while the Platform SDK tells you more about
"Integrity Levels" and how you can change them, the Framework has currently no support for
this, you will have to PInvoke the Win32 API's in order to change the Integrity Level.
Note that your idea to spawn IE and other applications AND control their lifetime is really
a bad idea, the option I suggested (running with Low Integrity) was only applicable in
relation with IE, however if you need to do this with Medium Integrity Level processes,
you'll have to run at Medium Level too. If you need both control Low and Medium Level,
you'll have to run as full Administrator, how you can do this is explained in above
document.

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

Process.ExitCode fails 2

Top