簡體   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