共用方式為


_onexit、_onexit_m

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

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

參數

  • function
    指向函式的指標在結束被呼叫。

傳回值

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

備註

_onexit 函式會傳遞 (function) 所呼叫的函式位址,當程式通常時結束。 成功呼叫_onexit會以LIFO(後進先出)順序產生執行目前函式的暫存器。 函式會傳遞至 _onexit 不能接受參數。

在這種情況下,當_onexit被DLL呼叫時,_onexit註冊的常式會在DLL_PROCESS_DETACH呼叫DllMain後在DLL卸載上執行。

_onexit 是一種Microsoft延伸。 若為 ANSI 可攜性,請使用 atexit。 函式的 _onexit_m 版本是使用混合模式。

需求

常式

必要的標頭

_onexit

<stdlib.h>

如需更多關於相容性的資訊,請參閱入門介紹中的 相容性 (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;
}

Output

This is executed first.
This is executed next.

.NET Framework 對等用法

System::Diagnostics::Process::Exited

請參閱

參考

流程控制和環境控制

atexit

exit、_exit

__dllonexit