我正在使用这段代码从GUI应用程序启动进程。但根据此代码的说明,无法从控制台应用程序启动进程。其实我想这样做,我想要一个控制台应用程序启动另一个控制台进程,请你有任何想法如何做到这一点?从控制台应用程序启动控制台应用程序
// This technique must be used for "console-less" parents such as GUI
// applications or detached applications.
// Using the STARTUPINFO STARTF_USESTDHANDLES flag, requires that
// the CreateProcess fInheritHandles parameter be set TRUE so that
// the file handles specified in the STARTUPINFO structure will be
// inherited by the child.
// setup the child process's handles for stdin, stdout, & stderr.
STARTUPINFO childProcStartupInfo;
memset(&childProcStartupInfo, 0, sizeof(childProcStartupInfo));
childProcStartupInfo.cb = sizeof(childProcStartupInfo);
childProcStartupInfo.hStdInput = hFromParent; // stdin
childProcStartupInfo.hStdOutput = hToParent; // stdout
childProcStartupInfo.hStdError = hToParentDup; // stderr
childProcStartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
childProcStartupInfo.wShowWindow = SW_HIDE;
// Now create the child process, inheriting handles
PROCESS_INFORMATION childProcInfo; /* for CreateProcess call */
bOk = CreateProcess(
NULL, // filename
pCmdLine, // full command line for child
NULL, // process security descriptor */
NULL, // thread security descriptor */
TRUE, // inherit handles? Also use if STARTF_USESTDHANDLES */
0, // creation flags */
NULL, // inherited environment address */
NULL, // startup dir; NULL = start in current */
&childProcStartupInfo, // pointer to startup info (input) */
&childProcInfo); // pointer to process info (output) */
创建从一个控制台应用程序(或任何其他应用程序)一个子进程是微不足道的。这样做同时继承stdout,stdin和stderr重定向的句柄完全是另一个问题,并且似乎是此代码的要点(和错误)。你可能想先阅读。 – WhozCraig
嗨克雷格,谢谢你的回复,但你有一段代码如何调用从控制台应用程序启动子进程?谢谢! – user1681210
*您*有一段代码启动子进程。你*没有*是一段代码,它以你想要的方式设置句柄继承。为此,我将你转到我的事先评论。 – WhozCraig