Archived

This topic is now archived and is closed to further replies.

After Exit function?

This topic is 5179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a windows program and I want to run a function after EVERYTHING else has been completed. At first I had a regular function call at the end of my program but that runs before global object destructors. Is there any "after exit" function? This is just for debugging. -Greg

Share this post


Link to post
Share on other sites
Maybe you need atexit?

Run-Time Library Reference

atexit

See Also
Process and Environment Control Routines | abort, exit | _onexit
Requirements
Routine Required header Compatibility
atexit <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

To generate an ANSI-compliant application, use the ANSI-standard atexit function (rather than the similar _onexit function).
Processes the specified function at exit.

int atexit(
void (__cdecl *func )( void )
);
Parameter
func
Function to be called.
Return Value
atexit returns 0 if successful, or a nonzero value if an error occurs.

Remarks
The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in last-in, first-out (LIFO) order. The functions passed to atexit cannot take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.

Requirements
Routine Required header Compatibility
atexit <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

To generate an ANSI-compliant application, use the ANSI-standard atexit function (rather than the similar _onexit function).

Example
/* ATEXIT.C: This program pushes four functions onto
* the stack of functions to be executed when atexit
* is called. When the program exits, these programs
* are executed on a "last in, first out" basis.
*/


#include <stdlib.h>
#include <stdio.h>

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

void main( void )
{
atexit( fn1 );
atexit( fn2 );
atexit( fn3 );
atexit( fn4 );
printf( "This is executed first.\n" );
}

void fn1()
{
printf( "next.\n" );
}

void fn2()
{
printf( "executed " );
}

void fn3()
{
printf( "is " );
}

void fn4()
{
printf( "This " );
}


Output
This is executed first.
This is executed next.

See Also
Process and Environment Control Routines | abort, exit | _onexit



--------------------------------------------------------------------------------

Send feedback to Microsoft

© 2001 Microsoft Corporation. All rights reserved.

Share this post


Link to post
Share on other sites