使 CreateProcess 继承调用进程的控制台 [英] Making CreateProcess inherit the console of the calling process

查看:16
本文介绍了使 CreateProcess 继承调用进程的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Windows 中调用 CreateProcess 时,新进程似乎没有继承调用进程的控制台.我制作了一个运行ruby xtest"的测试程序,xtest 是一个将hello"写入标准输出的脚本.我从 Emacs 运行了这个测试程序,但没有得到任何输出.我还尝试了以下调用 GetStdHandle 的代码,但同样没有输出.然后我尝试将 dwCreationFlags 中的 CREATE_NEW_CONSOLE 传递给 CreateProcess,它使用 Ruby 输出创建了一个全新的窗口.最后,我做了一个简单的 fork/exec测试程序并使用 Cygwin 的 GCC 编译它.该程序有效:Ruby 输出按预期显示在 Emacs 中.我试图在 http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/spawn.cc?rev=1.268&content-type=text/x-cvsweb-markup&cvsroot=src 但失败了.那么,如何让新进程继承父进程的控制台,让子进程的输出按预期显示呢?

When I call CreateProcess in Windows, the new process doesn't seem to inherit the console of the calling process. I made a test program that runs "ruby xtest", xtest being a script that writes "hello" to standard output. I ran this test program from Emacs, and get no output. I also tried the following code calling GetStdHandle, but again, no output. Then I tried passing CREATE_NEW_CONSOLE in dwCreationFlags to CreateProcess, which made a whole new window with the Ruby output. Finally, I made a simple fork/exec test program and compiled it using Cygwin's GCC. This program worked: the Ruby output showed up in Emacs as expected. I tried to decipher the Cygwin source code in http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/spawn.cc?rev=1.268&content-type=text/x-cvsweb-markup&cvsroot=src but failed. So, how do you make the new process inherit the console of the parent process such that the output from the child shows up as expected?

STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if(!CreateProcess(0, "ruby xtest", 0, 0, 1, 0, 0, 0, &si, &pi)) die("CreateProcess");

推荐答案

我知道这个帖子有点老了,但是我也遇到了同样的问题.

I know, this thread is rather old, however, I just ran into the same problem.

就像 TS 一样,控制台句柄是继承的,并且在 Cygwin 下可以正常工作,但在 Windows 控制台上却不行.相反,既没有显示 stdout 上的输出,也没有报告任何错误.继承的管道句柄仍然可以正常工作.

Just as for the TS, the console handle was inherited and working fine under Cygwin, but not on a Windows console. Instead, the output on stdout was neither shown, nor any error was reported. Inherited Pipe handles worked still fine.

我花了一些时间来确定(现在很明显的)问题:使用 CREATE_NO_WINDOW 调用了 CreateProcess().删除此标志,控制台输出就可以了.(不过,根据 TS 的代码,他们从一开始就没有设置这个标志.)

I took me some time to identify the (now obvious) problem: CreateProcess() was called with CREATE_NO_WINDOW. Dropping this flag, console output is fine. (Though, according to the code of the TS, they never set this flag in the first place.)

希望这可能对像我一样偶然发现此线程的人有所帮助.

Hope this might be helpful for people who also stumble across this thread, like myself.

这篇关于使 CreateProcess 继承调用进程的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆