2017-02-14 96 views
0

我想用我的MFC应用程序写入std::cerrstd::cout。在一个python脚本中,我称这个应用程序,我想从stdoutstderr读取。 两者都不起作用。仅使用std::cout就不会产生输出。在AllocConsole()之后,我至少能够打印到调试控制台。不幸的是,python网站上仍然没有输出。使用MFC写入stdout并通过python读取stdout Popen

在我的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(); 

蟒网站,我尝试打印输出。

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

有没有办法让我的MFC程序真正写入,并且Python脚本读取标准输出?

+0

尝试设置'标准错误= subprocess.PIPE'以及然后使用'(输出,错误)= process.communicate()'没有'read()'和'.wait()'。 – cdarke

+0

仍然没有输出:( – dgrat

+0

你测试输出和错误吗? – cdarke

回答

0

写入东西到stdout管的正确方法如下:

HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE); 
WriteFile(hStdOut, chBuf, dwRead, &dwWritten, NULL); 
相关问题