_onexit、_onexit_m_onexit, _onexit_m

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

语法Syntax

_onexit_t _onexit(  
   _onexit_t function  
);  
_onexit_t_m _onexit_m(  
   _onexit_t_m function  
);  

参数Parameters

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.

备注Remarks

当程序正常终止时,向 _onexit 函数传递要调用的函数 (function) 的地址。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.

如果从 DLL 范围内调用 _onexit,则在使用 DLL_PROCESS_DETACH 调用 _onexit 之后,向 DllMain 注册的例程将会在 DLL 卸载时运行。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.

惠?Requirements

例程所返回的值Routine 必需的标头Required header
_onexit <stdlib.h><stdlib.h>

有关更多兼容性信息,请参见“简介”中的 兼容性For more compatibility information, see Compatibility in the Introduction.

示例Example

// 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;  
}  

输出Output

This is executed first.  
This is executed next.  

请参阅See Also

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