_onexit, _onexit_m

在結束時註冊要呼叫的常式。

語法

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

參數

function
結束時要呼叫的函式指標。

傳回值

_onexit 如果成功,或 NULL 沒有儲存函式指標的空間,則會傳回函式指標的指標。

備註

當程式正常終止時,會將要呼叫之函式 (function) 的位址傳遞給 _onexit 函式。 後續呼叫 _onexit 會建立依 LIFO (後進先出) 順序執行之函式的暫存器。 傳遞至 _onexit 的函式無法接受參數。

在從 DLL 內呼叫 時 _onexit ,使用 卸載 DLL 之後,以 執行 DllMainDLL_PROCESS_DETACH 註冊 _onexit 的常式。

_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
exit, _Exit, _exit
__dllonexit