従来の RedirectIOToConsole 関数を使用して、Windows Visual 2012、リンカー/サブシステム = Windows (/SUBSYSTEM:WINDOWS) の出力コンソールでトレースをリダイレクトしようとしています。
AllocConsole の前に std::endl を実行すると、トレースの表示に問題が発生するようです。
以下は私のテストです:
#include <windows.h>
#include <stdio.h>
#include <iostream>
void RedirectIOToConsole()
{
FILE *conin, *conout;
AllocConsole();
freopen_s(&conin, "conin$", "r", stdin);
freopen_s(&conout, "conout$", "w", stdout);
freopen_s(&conout, "conout$", "w", stderr);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
// std::cout << "My Trace 1"; // Uncomment this line for Test 1
// std::cout << "My Trace 1" << std::endl; // Uncomment this line for Test 2
RedirectIOToConsole();
printf( "redirected console\n");
std::cout << "My Trace 2" << std::endl;
ch = getchar();
return 0;
}
- そのまま実行 - コンソール ウィンドウに出力:
リダイレクトされたコンソール
私のトレース 2 -> OK
- テスト 1 の行のコメントを外します - コンソール ウィンドウでの出力:
リダイレクトされたコンソール
私のトレース 2 -> OK
- テスト 2 の行のコメントを外します - コンソール ウィンドウでの出力:
リダイレクトされたコンソール -> NOK