Register a routine to be called at exit time.
#include <stdlib.h>
_onexit_t _onexit( _onexit_t func );
_fonexit_t __far _fonexit( _fonexit_t func );
func | Pointer to function to be called at exit |
The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that is executed in LIFO (last-in–first-out) order. Except for DOS32X, no more than 32 functions can be registered with _onexit; _onexit returns the value NULL if the number of functions exceeds 32. For DOS32X, more than 32 functions can be registered. Because the heap is used, the size of the function register is only limited by available memory in the heap. The functions passed to _onexit cannot take parameters.
The _fonexit function is a far version of _onexit; it can be used with any memory model.
Neither _onexit nor _fonexit is part of the ANSI definition; instead, both are Microsoft extensions. The ANSI-standard atexit function does the same thing as _onexit and should be used instead of _onexit when ANSI portability is desired.
Both _onexit and _fonexit return a pointer to the function if successful and return NULL if there is no space left to store the function pointer.
_onexit
Standards:UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Use _onexit for compatibility with ANSI naming conventions of non-ANSI functions. Use onexit and link with OLDNAMES.LIB for UNIX compatibility._fonexit
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
/* ONEXIT.C */
#include <stdlib.h>
#include <stdio.h>
/* Prototypes */
void fn1( void ), fn2( void ), fn3( void ), fn4( void );
void main( void )
{
_onexit( fn1 );
_onexit( fn2 );
_onexit( fn3 );
_onexit( fn4 );
printf( "This is executed first.\n" );
}
void fn1()
{
printf( "next.\n" );
}
void fn2()
{
printf( "executed " );
}
void fn3()
{
printf( "is " );
}
void fn4()
{
printf( "This " );
}
This is executed first.
This is executed next.