Board index » cppbuilder » Application return (error) code

Application return (error) code


2007-07-20 01:23:45 AM
cppbuilder95
Is there a way to make a cppbuilder app return an error code other than zero (0) to the operating system when it closes?
 
 

Re:Application return (error) code

"jzirbes" < XXXX@XXXXX.COM >wrote in message
Quote

Is there a way to make a cppbuilder app return an error code other than
zero (0) to the operating system when it closes?
Of course -- change the return value! For instance:
In GUI apps:
WINAPI WinMain(HINSTANCE HInstance, HINSTANCE, LPSTR, int)
{
return 1;
}
In console apps:
int main(int argc, char* argv[])
{
return 1;
}
- Dennis
 

Re:Application return (error) code

"jzirbes" < XXXX@XXXXX.COM >wrote in message
Quote
Is there a way to make a cppbuilder app return an
error code other than zero (0) to the operating
system when it closes?
You have to edit the project's WinMain() function. It has a 'return 0'
statement at the end. Simply change the value to whatever you need.
Gambit
 

{smallsort}

Re:Application return (error) code

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"jzirbes" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Is there a way to make a cppbuilder app return an
>error code other than zero (0) to the operating
>system when it closes?

You have to edit the project's WinMain() function. It has a 'return 0'
statement at the end. Simply change the value to whatever you need.


Gambit


I'm sorry, I should have been more explicit. I knew that I could manually change the value in the return statement. What I meant was, is there a way that this could be done from code so that if the application failed its purpose, I could close with and error code and have another application (a software installation application for instance) test this code to determine a course of action.
jzirbes
 

Re:Application return (error) code

"jzirbes" < XXXX@XXXXX.COM >wrote:
Quote
I'm sorry, I should have been more explicit. I knew that I could manually change the value in the return statement. What I meant was, is there a way that this could be done from code so that if the application failed its purpose, I could close with and error code and have another application (a software installation application for instance) test this code to determine a course of action.
To return a variable exit code, return a variable. (I'll assume you
understood this, and were actually more interested in the remainder of
the process.)
To create a process, use CreateProcess() or similar. This allows you to
determine the process handle
When the process has terminated, you can use the GetExitCodeProcess()
function on the process handle to determine its exit code.
See the help on the above functions for more details - that's where I
skimmed this from.
Alan Bellingham
--
Team Browns
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:Application return (error) code

Quote
I'm sorry, I should have been more explicit. I knew that I could manually
change the value in the return>statement. What I meant was, is there a
way that this could be done from code so that if the application failed
>its purpose, I could close with and error code and have another
application (a software installation application>for instance) test this
code to determine a course of action.
WINAPI WinMain(HINSTANCE HInstance, HINSTANCE, LPSTR, int)
{
int rv = 0;
try
{
// do something
}
catch(const Exception& e)
{
rv = 1;
}
catch(...)
{
rv = 2;
}
return rv;
}
 

Re:Application return (error) code

"jzirbes" < XXXX@XXXXX.COM >wrote in message
Quote
I knew that I could manually change the value in the return
statement. What I meant was, is there a way that this could
be done from code so that if the application failed its purpose,
I could close with and error code and have another application
(a software installation application for instance) test this code
to determine a course of action.
That is exactly what the return value in main/WinMain() is for. You don't
have to hard-code it. You can have it return the value from a variable that
the application assigns before exiting.
Gambit
 

Re:Application return (error) code

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"jzirbes" < XXXX@XXXXX.COM >wrote in message
news:469fcb0f$ XXXX@XXXXX.COM ...

>I knew that I could manually change the value in the return
>statement. What I meant was, is there a way that this could
>be done from code so that if the application failed its purpose,
>I could close with and error code and have another application
>(a software installation application for instance) test this code
>to determine a course of action.

That is exactly what the return value in main/WinMain() is for. You don't
have to hard-code it. You can have it return the value from a variable that
the application assigns before exiting.


Gambit


How do I declare a variable to assign as you suggest that is visible to the applications main cpp (ie: Project1.cpp) file.
I have tried using a header file with a variable definition
and include it in this file but I get multiple definition linker errors when I do this.
jzirbes
 

Re:Application return (error) code

"jzirbes" < XXXX@XXXXX.COM >wrote in message
Quote
How do I declare a variable to assign as you suggest that is visible to
the applications main cpp (ie: Project1.cpp) file.
I have tried using a header file with a variable definition
and include it in this file but I get multiple definition linker errors
when I do this.
In *one and only one* source file:
int MyReturnValue;
In a header file:
extern int MyReturnValue;
Then include the header file wherever you need access to 'MyReturnValue'.
- Dennis
 

Re:Application return (error) code

"jzirbes" < XXXX@XXXXX.COM >wrote in message
Quote
How do I declare a variable to assign as you suggest that is
visible to the applications main cpp (ie: Project1.cpp) file.
Define the variable in the .cpp file itself, and then use an 'extern'
statement in all of the other units that want to access it.
Quote
I have tried using a header file with a variable definition
and include it in this file but I get multiple definition linker
errors when I do this.
That is because you are declaring the actual variable in the header file, so
there will be multiple copies of it. That is where 'extern' statements come
into place. For example:
--- Project1.cpp ---
int MyReturnValue = 0;
WINAPI WinMain(HINSTANCE HInstance, HINSTANCE, LPSTR, int)
{
//...
return MyReturnValue;
}
--- Unit1.cpp ---
extern int MyReturnValue;
void __fastcall TForm1::ExitButtonClick(TObject *Sender)
{
if( some condition )
MyReturnValue = 1;
Close();
}
Gambit
 

Re:Application return (error) code

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"jzirbes" < XXXX@XXXXX.COM >wrote in message
news:46a4ec47$ XXXX@XXXXX.COM ...

>How do I declare a variable to assign as you suggest that is
>visible to the applications main cpp (ie: Project1.cpp) file.

Define the variable in the .cpp file itself, and then use an 'extern'
statement in all of the other units that want to access it.

>I have tried using a header file with a variable definition
>and include it in this file but I get multiple definition linker
>errors when I do this.

That is because you are declaring the actual variable in the header file, so
there will be multiple copies of it. That is where 'extern' statements come
into place. For example:

--- Project1.cpp ---

int MyReturnValue = 0;

WINAPI WinMain(HINSTANCE HInstance, HINSTANCE, LPSTR, int)
{
//...
return MyReturnValue;
}


--- Unit1.cpp ---

extern int MyReturnValue;

void __fastcall TForm1::ExitButtonClick(TObject *Sender)
{
if( some condition )
MyReturnValue = 1;
Close();
}


Gambit


Using the extern modifier works great, thanks for the help.
I'm not sure why I didn't think of it (duh!), Oh well...
FYI, I also found another way. The TApplication class has a
general purpose integer Tag property (Application->Tag). This
works the same but saves declaring any new variables. Again, thanks for your help.
jzirbes