注册在退出时要调用的例程。
语法
_onexit_t _onexit(
   _onexit_t function
);
_onexit_t_m _onexit_m(
   _onexit_t_m function
);
参数
function
指向在退出时要调用的函数的指针。
返回值
如果成功,则 _onexit 将返回一个指向此函数的指针;如果没有可用于存储此函数指针的空间,则为 NULL。
注解
当程序正常终止时,向 _onexit 函数传递要调用的函数 (function) 的地址。 对 _onexit 的后续调用将创建一个函数注册表,其中的函数按 LIFO(后进先出)顺序执行。 传递给 _onexit 的函数不能采用参数。
如果从 DLL 范围内调用 _onexit,则在使用 DLL_PROCESS_DETACH 调用 DllMain 之后,向 _onexit 注册的例程将会在 DLL 卸载时运行。
_onexit 是 Microsoft 扩展。 若要获得 ANSI 可移植性,请使用 atexit。 该函数的 _onexit_m 版本适用于混合模式。
要求
| 例程 | 必需的标头 | 
|---|---|
| _onexit | <stdlib.h> | 
有关兼容性的详细信息,请参阅 兼容性。
示例
// 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.
另请参阅
进程和环境控制
atexit
.- .
__dllonexit