Visual Studio 调试效率提升小Tips 桌面 MFC应用程序增加控制台窗口

发布时间 2023-05-29 09:29:44作者: GreyWang

Visual Studio IDE

调试问题

  • 调试时中文字符串显示乱码或者问号 => 将该变量添加到监视,在变量名后添加,s8

为 gui 程序添加 console 窗口打印信息

// wxh add console for print info!
if (!AllocConsole()) {
	printf_s("wxh open console failed!\n");
}
else {
	char szBuff[128];
	wsprintf(szBuff, "wxh debug output window, thread ID: %d\n", GetCurrentProcessId());
	SetConsoleTitleA(szBuff);
	freopen("conin$", "r+t", stdin);
	freopen("conout$", "w+t", stdout);
	freopen("conout$", "w+t", stderr);
	printf_s("wxh open console success!\n");
}

为桌面应用程序添加 console 窗口输出打印信息

  • "项目 -> 属性 -> 生成事件 -> 生成后事件 -> 命令行" 添加
  • editbin /SUBSYSTEM:CONSOLE $(OutDir)\$(ProjectName).exe
  • 调试运行会出现一个控制台窗口输出调试信息,一个正常运行的软件界面