WIndows:子进程创建新的控制台窗口,丢失标准输入/输出
我使用的是 Windows Vista 和 Python 2.7.2,但答案不一定是 Python 。
因此,我可以正常启动子进程 stdin/stdout 并与之交互(使用 python),对于诸如“dir”之类的命令行程序。
- 然而 -
我现在想要调用的程序喜欢在 Windows 上为自己创建一个新的控制台窗口(不是curses),并带有新的句柄,即使是从预先存在的 cmd.exe 窗口运行时也是如此。 (奇怪,因为它是 VLC 的“远程控制”界面。)有没有办法:
- 获取进程制作的控制台的标准输入/输出的句柄;或者
- 让新的 shell 在旧的 shell 中运行(就像从 bash 中调用 bash)?
如果做不到这一点,以便我可以破解子进程的代码,如何在 Windows 中设置新控制台并传输输入/输出?
编辑: 即
>>> p = Popen(args=['vlc','-I','rc'],stdin=PIPE,stdout=PIPE)
# [New console appears with text, asking for commands]
>>> p.stdin.write("quit\r\n")
Traceback:
File "<stdin>", line 1, in <module>
IOError: [Errno 22] Invalid argument
>>> p.stdout.readline()
''
>>> p.stdout.readline()
''
# [...]
但是出现的新控制台窗口也不接受键盘输入。
而通常情况下:
>>> p = Popen(args=['cmd'],stdin=PIPE,stdout=PIPE)
>>> p.stdin.write("dir\r\n")
>>> p.stdin.flush()
>>> p.stdout.readline() #Don't just do this IRL, may block.
'Microsoft Windows [Version...
I'm using Windows Vista and Python 2.7.2, but answers needn't be in Python.
So I can start and interact with a subprocesses stdin/stdout normally (using python), for command-line programs such as `dir'.
- however -
the program I now want to call likes to make a new console window for itself on Windows (not curses), with new handles, even when run from a pre-existing cmd.exe window. (Odd, as it's the "remote control" interface of VLC.) Is there any way of either:
- getting the handles for the process-made console's stdin/out; or
- getting the new shell to run within the old (like invoking bash from within bash)?
Failing that, so that I can hack the subprocesses' code, how would a new console be set up in Windows and in/output transferred?
Edit:
I.e.
>>> p = Popen(args=['vlc','-I','rc'],stdin=PIPE,stdout=PIPE)
# [New console appears with text, asking for commands]
>>> p.stdin.write("quit\r\n")
Traceback:
File "<stdin>", line 1, in <module>
IOError: [Errno 22] Invalid argument
>>> p.stdout.readline()
''
>>> p.stdout.readline()
''
# [...]
But the new console window that comes up doesn't accept keyboard input either.
Whereas normally:
>>> p = Popen(args=['cmd'],stdin=PIPE,stdout=PIPE)
>>> p.stdin.write("dir\r\n")
>>> p.stdin.flush()
>>> p.stdout.readline() #Don't just do this IRL, may block.
'Microsoft Windows [Version...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有获得在 Windows 上使用管道 stdin/stdout 的 rc 接口;在所有尝试
通信
或直接写入stdin
时,我都会收到IOError
。有一个选项--rc-fake-tty
可以让 rc 接口在 Linux 上编写脚本,但它在 Windows 中不可用——至少在我的有点过时的 VLC 版本 (1.1.4) 中不可用。另一方面,使用套接字接口似乎工作得很好。分配给startupinfo选项并由Win32 CreateProcess函数使用的结构可以配置为隐藏进程窗口。但是,对于 VLC rc 控制台,我认为使用现有的
--rc-quiet
选项更简单。一般来说,以下是如何配置startupinfo
来隐藏进程窗口:为了完整起见——以防在您的系统上使用管道也失败——这是我使用
编写的一个小演示>--rc-host
选项使用套接字进行通信。它还使用--rc-quiet
来隐藏控制台。这只是打印帮助并退出。我没有测试过其他任何东西。我检查它在 Python 版本 2.7.2 和 3.2.2 中是否有效。 (我知道您没有要求这个,但也许它对您仍然有用。)I haven't gotten the rc interface to work with a piped stdin/stdout on Windows; I get
IOError
at all attempts tocommunicate
or write directly tostdin
. There's an option--rc-fake-tty
that lets the rc interface be scripted on Linux, but it's not available in Windows -- at least not in my somewhat dated version of VLC (1.1.4). Using the socket interface, on the other hand, seems to work fine.The structure assigned to the
startupinfo
option -- and used by the Win32CreateProcess
function -- can be configured to hide a process window. However, for the VLC rc console, I think it's simpler to use the existing--rc-quiet
option. In general, here's how to configurestartupinfo
to hide a process window:Just to be complete -- in case using pipes is failing on your system too -- here's a little demo I cooked up using the
--rc-host
option to communicate using a socket. It also uses--rc-quiet
to hide the console. This just prints the help and quits. I haven't tested anything else. I checked that it works in Python versions 2.7.2 and 3.2.2. (I know you didn't ask for this, but maybe it will be useful to you nonetheless.)参考监视出现在新生成的控制台窗口中的 stdOut。
这是另一个问题/答案 就解决了这个问题。
总之(如 Adam MW 的回答):
--intf=dummy --dummy-quiet
或--intf=rc --rc-quiet
。注意:对于rc接口的stdIn命令,
--rc-host
解决方案由eryksun的回答With reference to monitoring the stdOut which appears in the new Spawned Console Window.
Here´s another question/answer that solves the problem.
In summary (as answered by Adam M-W ):
--intf=dummy --dummy-quiet
or--intf=rc --rc-quiet
.Note: As for stdIn commands for the rc interface, the
--rc-host
solution is described by eryksun´s answer