给Win32 GUI程序调试信息输出方法


给Win32 GUI程序调试信息输出方法
2011年09月23日
   DebugView可以追踪TRACE(实际上是Windows函数OutputDebugString)的一个工具.
  ======================================================================
  在Win32的GUI程序中,没有控制台窗口,我们输出调试信息时有些不方便,以往我的做法是使用MessageBox弹出对话框、OutputDebugString向VS的调试窗口输出信息等,但总感觉不如控制台输出几句printf有效率。经过尝试发现GUI程序照样能够拥有(且仅能够拥有一个)控制台窗口,具体做法如下:
  #include
  在程序初始化时,加入以下代码:
  FILE* fpDebugOut = NULL;
  FILE* fpDebugIn   = NULL;
  if( !AllocConsole() ) MessageBox(NULL, _T("控制台生成失败。"), NULL, 0);
  SetConsoleTitle(_T("Debug Window"));
  _tfreopen_s(&fpDebugOut, _T("CONOUT$"),_T("w"), stdout);
  _tfreopen_s(&fpDebugIn, _T("CONIN$"), _T("r"), stdin);
  _tsetlocale(LC_ALL, _T("chs"));     //这是必要的,否则unicode模式下使用C库函数控制台输出不了中文 WriteConsole函数不受影响
  注意AllocConsole可能失败,原因可能是程序已经于一个控制台窗口关联过了,等等。
  程序结束时,加入以下代码:
  fclose(fpDebugOut);
  fclose(fpDebugIn);
  FreeConsole();
  好,现在已经加入了控制台,可以输出字符串了,使用标准的C库函数或者WriteConsole都可以:
  使用WriteConsole的话前面其实不用调用freopen重定向输出流了,直接WriteConsole就行:
  TCHAR str[256];
  DWORD numOfCharWritten = 0;
  _stprintf_s(str, 256, _T("今年是 %d 年\n"), 2008);
  WriteConsole( GetStdHandle(STD_OUTPUT_HANDLE), str, _tcslen(str), &numOfCharWritten, NULL);
  使用C库函数则更简单,但上面一定要调用freopen才有效:
  int tmp = 0;
  _tscanf_s(_T("%d\n"), &tmp);
  _tprintf_s(_T("刚才输入的是 %d\n"), tmp);
  比如在响应WM_PAINT消息时,加入_tprintf_s(_T("重绘 %d 次\n"), ++count),则每次窗口绘制的时候都将输出总重绘次数。
  最后,定义两个宏图省事:
  #define CREATE_DEBUG_CONSOLE \
  FILE* fpDebugOut = NULL; \
  FILE* fpDebugIn = NULL; \
  FILE* fpDebugErr = NULL; \
  if( !AllocConsole() ) \
  MessageBox(NULL, _T("控制台生成失败。"), NULL, 0); \
  SetConsoleTitle(_T("Debug Window")); \
  _tfreopen_s(&fpDebugOut, _T("CONOUT$"),_T("w"), stdout); \
  _tfreopen_s(&fpDebugIn, _T("CONIN$"), _T("r"), stdin); \
  _tfreopen_s(&fpDebugErr, _T("CONOUT$"),_T("w"), stderr); \
  _tsetlocale(LC_ALL, _T("chs"));
  #define RELEASE_DEBUG_CONSOLE \
  fclose(fpDebugOut); \
  fclose(fpDebugIn); \
  fclose(fpDebugErr); \
  FreeConsole();

猜你喜欢

转载自qdnng95r.iteye.com/blog/1359307
今日推荐