从python中读取外部stdout流

时间:2014-11-20 19:58:56

标签: python c++ windows pywin32

我希望我不会让事情变得混乱。我想从外部程序(带有外部SDK的C ++程序)读取stdout流,该程序当前将其printf命令发送到win32控制台窗口(Visual Studio的标准输出窗口)。它是Active(Win32)模式下的应用程序(.exe),并打开一个单独的cmd窗口来显示输出(不在visual studio调试器中)。

我想从外部python脚本捕获正在该程序中执行的printf命令。大多数解决方案/问题,我看过使用Popen,子进程或ctypes,只有在Cthon(而不是单独)中启动C / C ++程序时才能工作。

我看了这个问题。 How to capture python interpreter and CMD output from Python

它用于monkeypatching的代码片段之一说可以做到这一点 -

import sys
import StringIO

s = StringIO.StringIO()

sys.stdout = s

print "hey, this isn't going to stdout at all!"
print "where is it ?"

sys.stderr.write('It actually went to a StringIO object, I will show you now:\n')
sys.stderr.write(s.getvalue())

然而,这只适用于我在python解释器中做的任何事情。根据我的理解,sys.stdout是特定于解释器的,每当我将sys.stdout设置为StringIO对象时,它只会将在该python解释器中启动的stdout重定向到StringIO对象。

如何将此功能扩展到其他CMD / wind32窗口?

我已经考虑从python中启动C应用程序,并且知道如何使用该技术重定向输出。但是,我希望使用Visual Studio单独启动我的C / C ++应用程序,以及python脚本能够从中读取内容的方法。

非常感谢任何有用的指示!

编辑:编辑了描述,使视觉工作室部分更加清晰。

0 个答案:

没有答案
相关问题