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

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

问题描述

当我在Windows中调用CreateProcess时,新进程似乎不继承调用进程的控制台。我做了一个运行ruby xtest的测试程序,xtest是一个脚本,将hello写入标准输出。我从Emacs运行这个测试程序,并得不到输出。我也试过下面的代码调用GetStdHandle,但是,再次,没有输出。然后我尝试将dwCreationFlags中的CREATE_NEW_CONSOLE传递给CreateProcess,这使得一个新的窗口与Ruby输出。最后,我做了一个简单的fork / exec
测试程序,并使用Cygwin的GCC编译它。这个程序工作:Ruby输出出现在Emacs按预期。我试图解密Cygwin源代码在 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.

我花了一些时间来识别(现在是明显的)问题:CreateProcess()是用CREATE_NO_WINDOW调用的。删除此标志,控制台输出正常。 (虽然,根据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天全站免登陆