用MFC写入stdout,用python Popen读取stdout

2024-04-28 18:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用MFC应用程序写入std::cerrstd::cout。在python脚本中,我调用这个应用程序,并希望从stdoutstderr读取。 两者都不起作用。仅仅使用std::cout不会产生任何输出。在AllocConsole()之后,我至少能够打印到调试控制台。不幸的是,python站点上仍然没有输出。你知道吗

在MFC应用程序中,我使用以下代码初始化要写入的控制台:

void BindStdHandlesToConsole()
{
  // Redirect the CRT standard input, output, and error handles to the console
  freopen("CONIN$", "r", stdin);
  freopen("CONOUT$", "w", stdout);
  freopen("CONOUT$", "w", stderr);

  std::wcout.clear();
  std::cout.clear();
  std::wcerr.clear();
  std::cerr.clear();
  std::wcin.clear();
  std::cin.clear();
}

// initialization
BOOL foo::InitInstance()
{
  // allocate a console
  if (!AllocConsole())
    AfxMessageBox("Failed to create the console!", MB_ICONEXCLAMATION);
  else 
    BindStdHandlesToConsole();

在python站点上,我尝试打印输出。你知道吗

process = subprocess.Popen(args,stdout=subprocess.PIPE,shell=True)    
output = process.stdout.read()
process.wait()

有没有办法让我的MFC程序真正写出来,让python脚本读取标准输出?你知道吗


Tags: the脚本应用程序站点stderrstdoutprocessconsole