在基于MFC对话框的应用程序中处理控制台关

时间:2015-04-23 15:11:50

标签: c++ mfc console stdout

我编写的代码分配了一个控制台,并将基于MFC对话框的程序的输出重定向到该控制台。如果我关闭控制台窗口,它将关闭我的程序,我得到一个大的内存泄漏。有没有办法监听控制台关闭并停止向其发送数据?下面是我分配控制台并写入它的代码片段。在我的析构函数中,我调用了FreeConsole()

if (consoleEnabled)
{
  AllocConsole(); //Allocate the console

  FILE* pCout;

  if (freopen_s(&pCout, "CONOUT$", "w", stdout) != 0)     //redirect the stdout
  {
    this->consoleEnabled = false;
  }                
}

//in another function...
if (consoleEnabled)
{
  std::cout << outputMsg << std::endl;
}

0 个答案:

没有答案
相关问题