繁体   English   中英

使用MFC写入标准输出,并通过python Popen读取标准输出

[英]Writing into stdout with MFC and read stdout by python Popen

我想用我的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脚本读取标准输出?

将内容写入stdout管道的正确方法如下:

HANDLE hStdOut = GetStdHandle ( STD_OUTPUT_HANDLE );
WriteFile( hStdOut, chBuf, dwRead, &dwWritten, NULL );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM