_onexit、_onexit_m_onexit, _onexit_m

注册在退出时要调用的例程。Registers a routine to be called at exit time.


_onexit_t _onexit(
   _onexit_t function
_onexit_t_m _onexit_m(
   _onexit_t_m function


指向在退出时要调用的函数的指针。Pointer to a function to be called at exit.

返回值Return Value

_onexit将指针返回到该函数,如果成功或NULL如果没有任何空间来存储函数的指针。_onexit returns a pointer to the function if successful or NULL if there is no space to store the function pointer.


_Onexit函数传递的函数地址 (函数) 在程序正常终止时调用。The _onexit function is passed the address of a function (function) to be called when the program terminates normally. 对连续调用 _onexit创建按 LIFO (后进中第一个-出) 顺序执行的函数的寄存器。Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. 函数传递给 _onexit不能接受参数。The functions passed to _onexit cannot take parameters.

在这种情况时 _onexit从 DLL,注册的例程中调用 _onexit在 DLL 上的运行卸载后DllMain使用 DLL_PROCESS_DETACH 调用。In the case when _onexit is called from within a DLL, routines registered with _onexit run on a DLL's unloading after DllMain is called with DLL_PROCESS_DETACH.

_onexit是 Microsoft 扩展。_onexit is a Microsoft extension. 若要获得 ANSI 可移植性,请使用 atexitFor ANSI portability, use atexit. _Onexit_m该函数的版本为混合模式下使用。The _onexit_m version of the function is for mixed mode use.


例程Routine 必需的标头Required header
_onexit_onexit <stdlib.h><stdlib.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


// crt_onexit.c

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

/* Prototypes */
int fn1(void), fn2(void), fn3(void), fn4 (void);

int main( void )
   _onexit( fn1 );
   _onexit( fn2 );
   _onexit( fn3 );
   _onexit( fn4 );
   printf( "This is executed first.\n" );

int fn1()
   printf( "next.\n" );
   return 0;

int fn2()
   printf( "executed " );
   return 0;

int fn3()
   printf( "is " );
   return 0;

int fn4()
   printf( "This " );
   return 0;


This is executed first.
This is executed next.

请参阅See also

进程和环境控制Process and Environment Control
exit、_Exit、_exitexit, _Exit, _exit