473,796 Members | 2,839 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
11 29743
Peter Steele wrote:

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


Ok, if that is what you are after, why not just use the following:

Process app = Process.Start(" MyApp.exe");
app.WaitForExit (); // or wait for exit event, etc.
int code = app.ExitCode;
Nov 16 '05 #11
I understand what you mean now - but I think the point is that while the
message box is still on the screen the application has NOT exited and so has
NOT yet returned an exit code to Windows (even though it has returned
control to the command prompt, it is still running). Maybe this will be
clearer if you change your code to:
static int Main()
{
MessageBox.Show ("App starting");
MessageBox.Show ("App stopping");
return 1;
}
You should find that you don't see the second message box until you've
pressed OK on the first one, and similarly the "return 1;" is not executed
until you've pressed OK on the second one.

From this I suspect the problem is with how are you trying to use the exit
code, rather than with how you are setting it. As someone else has
suggested, if you are running it from a command prompt use "start /wait" (if
you type "start /?" the help displayed includes this: "When executing an
application that is a 32-bit GUI application, CMD.EXE does not wait for the
application to terminate before returning to the command prompt. This new
behavior does NOT occur if executing within a command script."). If you're
actually trying to test the exit code in some other way (e.g. from within
another program) then please post an example.

Chris Jobson

----- Original Message -----
From: "Peter Steele" <ps*****@z-force.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
Sent: Tuesday, September 28, 2004 6:11 PM
Subject: Re: How to set exit code of non-console app?

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?

Nov 16 '05 #12

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

Similar topics

1
4679
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
1687
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
3741
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
2687
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 ways? Regards, Iker
4
20318
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
2019
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
10127
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)) LOG("Expand error %ld", dwExit); This usually works fine but in several occasions I got in my logs...
2
14870
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 sys.exit(256).
4
6194
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 applications for Windows using Java. They were able to launch the Python script from within Java by...
9
11584
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
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.