_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 受け取ることはできません。

DLL 内から呼び出された _onexit 場合、DLL がアンロードされたときに登録された _onexit ルーチンは、 DllMain 後で呼び出 DLL_PROCESS_DETACHされます。

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

出力

This is executed first.
This is executed next.

関連項目

プロセスと環境の制御
atexit
exit, _Exit, _exit
__dllonexit