次の方法で共有


_onexit、_onexit_m

更新 : 2007 年 11 月

終了時に呼び出すルーチンを登録します。

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

パラメータ

  • function
    終了時に呼び出す関数へのポインタ。

戻り値

正常に終了した場合、_onexit は関数へのポインタを返します。関数ポインタを格納する領域がない場合は NULL を返します。

解説

_onexit 関数には、プログラムの正常終了時に呼び出す関数 (function) のアドレスが渡されます。_onexit を連続して呼び出すと、後入れ先出し (LIFO: last-in-first-out) の順で実行される関数が登録されます。_onexit に渡される関数はパラメータを取ることができません。

_onexit を DLL の内部から呼び出す場合は、DLL_PROCESS_DETACH で DllMain を呼び出した後に、_onexit に登録されたルーチンが DLL のアンロード処理時に実行されます。

_onexit は、Microsoft の拡張機能です。ANSI 仕様による移植性が必要な場合は、atexit を使用します。関数の _onexit_m バージョンは、混合モードで使用します。

必要条件

ルーチン

必須ヘッダー

_onexit

<stdlib.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

// 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.

.NET Framework の相当するアイテム

System::Diagnostics::Process::Exited

参照

参照

プロセス制御と環境制御

atexit

exit、_exit