Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching exit code before program termination 1

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
0
0
US
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:

Code:
void exithandler()
{
	DWORD exitCode = 0;
	::GetExitCodeProcess(GetCurrentProcess(),&exitCode);
	std::cout << exitCode << std::endl;
        // prints 259, NOT 55
}


int _tmain(int argc, _TCHAR* argv[])
{
	atexit(exithandler);
	exit(55);
	return 0;
}

This is for Windows, but I am looking for a solution for UNIX/Linux. BTW... this solution does NOT work because the process has not yet terminated when my exit handler is called, thus resulting in exitCode of 259 instead of 55. 259 on Windows means the process is still active.

Does anyone know if this can be done?
Use case: Imagine you have a library and you want to check if someone is calling exit with a particular code.
 
The only portable way to do this would be to use two processes. The parent process launches the child process, then just waits for it to exit, then picks up the final exit status to do whatever it is you want to do with it.

Or you may be able to override the exit() function with your own wrapper function, which could then store the apparent exit status in a location where you could find it later.

You could probably do what you want, but it wouldn't be pretty or portable.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top