473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

CloseMainWindow() doesn't appear to work under Vista

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);
}
}

--
-----------
Thanks,
Steve
Apr 18 '07 #1
6 3944
"SteveT" <St****@newsgroups.nospamwrote in message
news:2F**********************************@microsof t.com...
>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);
}
}

--
-----------
Thanks,
Steve

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

Willy.

Apr 18 '07 #2
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Apr 19 '07 #3
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?
--
-----------
Thanks,
Steve
"Linda Liu [MSFT]" wrote:
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Apr 19 '07 #4
"SteveT" <St****@newsgroups.nospamwrote in message
news:47**********************************@microsof t.com...
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/downlo...UACDevReqs.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.

Apr 20 '07 #5
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?
--
-----------
Thanks,
Steve
"Willy Denoyette [MVP]" wrote:
"SteveT" <St****@newsgroups.nospamwrote in message
news:47**********************************@microsof t.com...
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/downlo...UACDevReqs.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.

Apr 20 '07 #6
"SteveT" <St****@newsgroups.nospamwrote in message
news:93**********************************@microsof t.com...
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?
--
-----------
Thanks,
Steve

It's really important to fully understand both UAC and Integrity levels, before starting
development on Vista.
This document
<http://download.microsoft.com/download/5/6/a/56a0ed11-e073-42f9-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.

Apr 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ron Bromwell | last post by:
Hi All, I'm trying to gracefully run all running processes for an application. The problem I am running into in multiple people will be logged in for the app. I am currently using: For Each...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
3
by: Andy | last post by:
Is there a way to force a window to close without displaying a popup asking: "Windows cannot end this program....If you choose to end the program immediately, you will lose any unsaved data." ...
23
by: jim | last post by:
Look at Microsoft and Windows apps in Vista.... They aren't .Net. If .Net is so great, why doesn't MS use it more? In fact, what desktop applications does Microsoft code in .Net? I think...
0
by: jcvoon | last post by:
Hi all: The application is a WinForm application which will invoke the Web Services host in the same development machine, the WSE policy is setup properly and it is configure to use...
0
by: =?Utf-8?B?VHJhY2tz?= | last post by:
I created a setup program in VS2005 ide. It installs my app on 98-Vista. I have tried this with the Click once check box checked(full trust) and not checked does not seem to matter. Anyway, the...
1
by: teddysnips | last post by:
I have a new client who has an Access 2000 database that used to run under Windows 2000 Pro. They have just "upgraded" to Access 2007 under Vista, and the old database stopped working - buttons on...
1
by: =?Utf-8?B?TFQgS2hvbw==?= | last post by:
Hello everybody, I had a window service program that invokes command scripts from bat/cmd files using the C function "system", compiled using Visual Studio 2005. When the scripts are invoked in...
12
by: nik | last post by:
Hi, I've compiled my application on my vista machine, and it won't run at all on my xp machine. In the windows error report I get Exception code; 0xe0434f4d. I searched for that exception, but...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.