コンソールアプリをデバッグ実行するときに、勝手に終了させないようにする(VC6)

単なる実行(Ctrl+F5)だとmainを抜けてもコマンドプロンプトが残ってくれるが、デバッグ実行(F5)をすると秒速で消えてしまって、printfした結果が見れない。
ユニットテストを書いているときは、いつでも最後に結果を目視したい。

なので次のように書いてみた。

//include IsDebbugerPresent() API in winbase.h
#if (_WIN32_WINNT < 0x0400)
	#define _WIN32_WINNT 0x0400
#endif

#include <stdio.h>
#include <conio.h>		//_getch()
#include <windows.h>

//If running in debbuffer, wait for key input.
void waitKeyInputIfRunningInDebugger(){
	if (::IsDebuggerPresent()){
		printf("Press any key to continue");
		_getch();
	}
}

int main(int argc, char* argv[])
{
	printf("test OK!\n");
	waitKeyInputIfRunningInDebugger();
	return 0;
}

IsDebuggerPresent()はWinNTかWin98以降のサポートなので、先頭に余計な#ifdefが必要。


これで、デバッグ実行でも直接実行でも同じ挙動になる。