473,554 Members | 3,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to set exit code of non-console app?

I have a Windows application written in C# that I want to return a non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is
there a way to change the value a Windows application exits with. Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches to
return a non-zero exit code.
Nov 16 '05 #1
11 29668
Try setting Environment.Exi tCode

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is there a way to change the value a Windows application exits with. Basically I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches to return a non-zero exit code.

Nov 16 '05 #2
I'd already found this but it doesn't seem to have any effect. I added the
line

Environment.Exi tCode = 1;

in Main but the application still exited with 0. I tried putting it in the
app's main constructor and this still didn't have any effect. If I compile
the app as a console application this works but not as a Windows
application.

So where am I supposed to set this ExitCode property?

"Michael C" <mi******@nospa m.org> wrote in message
news:da******** **************@ news4.srv.hcvln y.cv.net...
Try setting Environment.Exi tCode

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a Windows application written in C# that I want to return a

non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open.

Is
there a way to change the value a Windows application exits with.

Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches

to
return a non-zero exit code.


Nov 16 '05 #3
Try putting that line in the .Closing event of your form.

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:Oe******** *******@TK2MSFT NGP11.phx.gbl.. .
I'd already found this but it doesn't seem to have any effect. I added the
line

Environment.Exi tCode = 1;

in Main but the application still exited with 0. I tried putting it in the app's main constructor and this still didn't have any effect. If I compile
the app as a console application this works but not as a Windows
application.

So where am I supposed to set this ExitCode property?

"Michael C" <mi******@nospa m.org> wrote in message
news:da******** **************@ news4.srv.hcvln y.cv.net...
Try setting Environment.Exi tCode

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a Windows application written in C# that I want to return a

non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open.
Is
there a way to change the value a Windows application exits with.

Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the
exit code and this particular application expects that the program it

launches to
return a non-zero exit code.



Nov 16 '05 #4
You could do Environment.Exi t(exitcode) but this method isn't recommended
for non console apps because it doesn' run the winforms closing/exit
methods/events properly.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Peter Steele" <ps*****@z-force.com> schrieb im Newsbeitrag
news:#b******** ******@TK2MSFTN GP09.phx.gbl...
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is there a way to change the value a Windows application exits with. Basically I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches to return a non-zero exit code.

Nov 16 '05 #5
I've tried calling Environment.Exi t(exitcode) but the app still returns an
exit code of 0, regardless of what I specify in the Exit call.

Peter

"cody" <no************ ****@gmx.net> wrote in message
news:uq******** ******@TK2MSFTN GP09.phx.gbl...
You could do Environment.Exi t(exitcode) but this method isn't recommended
for non console apps because it doesn' run the winforms closing/exit
methods/events properly.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Peter Steele" <ps*****@z-force.com> schrieb im Newsbeitrag
news:#b******** ******@TK2MSFTN GP09.phx.gbl...
I have a Windows application written in C# that I want to return a

non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open.

Is
there a way to change the value a Windows application exits with.

Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches

to
return a non-zero exit code.


Nov 16 '05 #6
As I understand it the result of Main() is the application's exit code. If I
use Visual Studio to create a C# Windows Application and then modify Main()
to be:
[STAThread]
static int Main() {
Application.Run (new Form1());
return 1;
}
it seems to exit with an exit code of 1. Maybe I'm missing something because
I don't know what you mean by "Windows applications exit immediately,
leaving their windows still open." Surely if there are still windows open
then the application can't have exited (unless the windows are created by a
different thread - I don't know what happens in that case when the initial
thread exits).

Chris Jobson

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a Windows application written in C# that I want to return a non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is
there a way to change the value a Windows application exits with. Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches
to return a non-zero exit code.

Nov 16 '05 #7
Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show ("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe

the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code is
always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code of
the application. If I change the application type to "Console application"
instead of "Windows application" via the project's property pages, in this
case the the MessageBox call causes the program to suspend execution and the
command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application is
set through some kind of OS magic and it doesn't appear to be able to be
controlled in the normal manner. Is there a way to set it?

"Chris Jobson" <ch**********@b tinternet.com> wrote in message
news:uG******** ******@TK2MSFTN GP15.phx.gbl...
As I understand it the result of Main() is the application's exit code. If
I use Visual Studio to create a C# Windows Application and then modify
Main() to be:
[STAThread]
static int Main() {
Application.Run (new Form1());
return 1;
}
it seems to exit with an exit code of 1. Maybe I'm missing something
because I don't know what you mean by "Windows applications exit
immediately, leaving their windows still open." Surely if there are still
windows open then the application can't have exited (unless the windows
are created by a different thread - I don't know what happens in that case
when the initial thread exits).

Chris Jobson

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a Windows application written in C# that I want to return a
non-zero exit code when it it run. The problem is that as a Windows
application , there doesn't seem to be a way to control this. The value
returned by the Main function has no impact on the value returned by the
application . Windows applications exit immediately, leaving their windows
still open. Is there a way to change the value a Windows application exits
with. Basically I want it to return 1 instead of 0. I need to do this
because the application is launched by another application that checked
for the exit code and this particular application expects that the program
it launches to return a non-zero exit code.


Nov 16 '05 #8
Peter Steele wrote:

Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show ("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe
Do this instead, and then report back:

C:\>start /wait MyApp.exe
C:\>echo %errorlevel%


the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code is
always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code of
the application. If I change the application type to "Console application"
instead of "Windows application" via the project's property pages, in this
case the the MessageBox call causes the program to suspend execution and the
command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application is
set through some kind of OS magic and it doesn't appear to be able to be
controlled in the normal manner. Is there a way to set it?

"Chris Jobson" <ch**********@b tinternet.com> wrote in message
news:uG******** ******@TK2MSFTN GP15.phx.gbl...
As I understand it the result of Main() is the application's exit code. If
I use Visual Studio to create a C# Windows Application and then modify
Main() to be:
[STAThread]
static int Main() {
Application.Run (new Form1());
return 1;
}
it seems to exit with an exit code of 1. Maybe I'm missing something
because I don't know what you mean by "Windows applications exit
immediately, leaving their windows still open." Surely if there are still
windows open then the application can't have exited (unless the windows
are created by a different thread - I don't know what happens in that case
when the initial thread exits).

Chris Jobson

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a Windows application written in C# that I want to return a
non-zero exit code when it it run. The problem is that as a Windows
application , there doesn't seem to be a way to control this. The value
returned by the Main function has no impact on the value returned by the
application . Windows applications exit immediately, leaving their windows
still open. Is there a way to change the value a Windows application exits
with. Basically I want it to return 1 instead of 0. I need to do this
because the application is launched by another application that checked
for the exit code and this particular application expects that the program
it launches to return a non-zero exit code.


Nov 16 '05 #9
Okay, this works. It has a side effect though. I need a command that can be
specified in code and

system("start /wait MyApp.exe");

doesn't work. This has to be written as

system("cmd /c\" start /wait MyApp.exe\"");

This has a side effect of throwing up a console window, which is exactly
what I was trying to avoid. I can get around this I guess by using
CreateProcess and telling it to hide the window...

"Julie" <ju***@nospam.c om> wrote in message
news:41******** *******@nospam. com...
Peter Steele wrote:

Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show ("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe


Do this instead, and then report back:

C:\>start /wait MyApp.exe
C:\>echo %errorlevel%


the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code
is
always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code
of
the application. If I change the application type to "Console
application"
instead of "Windows application" via the project's property pages, in
this
case the the MessageBox call causes the program to suspend execution and
the
command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application
is
set through some kind of OS magic and it doesn't appear to be able to be
controlled in the normal manner. Is there a way to set it?

"Chris Jobson" <ch**********@b tinternet.com> wrote in message
news:uG******** ******@TK2MSFTN GP15.phx.gbl...
> As I understand it the result of Main() is the application's exit code.
> If
> I use Visual Studio to create a C# Windows Application and then modify
> Main() to be:
> [STAThread]
> static int Main() {
> Application.Run (new Form1());
> return 1;
> }
> it seems to exit with an exit code of 1. Maybe I'm missing something
> because I don't know what you mean by "Windows applications exit
> immediately, leaving their windows still open." Surely if there are
> still
> windows open then the application can't have exited (unless the windows
> are created by a different thread - I don't know what happens in that
> case
> when the initial thread exits).
>
> Chris Jobson
>
> "Peter Steele" <ps*****@z-force.com> wrote in message
> news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
>>I have a Windows application written in C# that I want to return a
>>non-zero exit code when it it run. The problem is that as a Windows
>>application , there doesn't seem to be a way to control this. The value
>>returned by the Main function has no impact on the value returned by
>>the
>>application . Windows applications exit immediately, leaving their
>>windows
>>still open. Is there a way to change the value a Windows application
>>exits
>>with. Basically I want it to return 1 instead of 0. I need to do this
>>because the application is launched by another application that checked
>>for the exit code and this particular application expects that the
>>program
>>it launches to return a non-zero exit code.
>
>

Nov 16 '05 #10

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

Similar topics

1
4666
by: Mark McEahern | last post by:
Is there a straightforward way to get the exit code from a windows process? I want to write a script that runs a bunch of .sql files against SQL Server using osql. I want it to stop if there's an error; e.g., import os import glob server = 'foo'
3
1675
by: Micus | last post by:
Greetings, I need to execute 2 applications from an application or batch file. The first app launched has the user enter a product key and the app's exit code will be 0 (failed) or 1 (succeeded). Based on the exit code of the first app, the second app will be run. My problems : Using a batch file to launch the applications produces an
8
3730
by: Atanas Banov | last post by:
i ran onto this weirdness today: seems like close() on popen-ed (pseudo)file fails miserably with exception instead of returning exit code, when said exit code is -1. here is the simplest example (under Windows): >>> print popen('exit 1').close() 1 >>> print popen('exit -1').close() Traceback (most recent call last):
2
2672
by: iker.arizmendi | last post by:
On UNIX one can use popen* to get a pipe for reading, a pipe for writing, and the exit code of the child process via a call to close() on the last pipe. Is there any way, in principle, to simulate such behaviour on Windows? Some googling reveals that direct use of the popen* functions on Windows will not do the trick, but are there indirect...
4
20274
by: eva.monsen | last post by:
I'm trying to run a .BAT file using System.Diagnostics.Process. I'm having trouble getting Process.ExitCode to match up with what the .BAT file returns. Here are the contents of C:\temp\ConsoleApplication1\ConsoleApplication1\test1.bat: exit /B 6 Here is some C# code:
1
2007
by: Liang Zhang | last post by:
Hi, All in my project, i need to call a java method from C code using JNI, yet I do not know how to retrieve the exit code of java program. the java code is like below ..... public static void main(....) { ...... if(error){
7
10114
by: Benzi Eilon | last post by:
I am executing an Expand command in order to expand a CAB file. This is done by calling CreateProcess with the Expand command and then WaitForSingleObject and checking the process exit code. My C++ code for checking the exit code looks something like this: DWORD dwExit = 0; if ((GetExitCodeProcess(hProcess, &dwExit)) && (dwExit != 0))...
2
14827
by: sunyao | last post by:
Hi Friend, I write a linux shell script to call a python script inside. But I got a wrong exit code from Python exit code. The linux shell script as follows; #!/bin/bash MyPython.py exit $? After run the above linux script, I always got exit code 0. But autually the Python log file shows that its exit code is 256. In python script, use...
4
6174
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages generated are written to a log file. A project team working in the same company as me here would like to use this loading utility. They write UI...
9
11545
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something like this: void exithandler() { DWORD exitCode = 0;
0
7584
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7783
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8023
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5144
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1117
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
828
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.