C++ 从控制台应用程序启动控制台应用程序

C++ 从控制台应用程序启动控制台应用程序,c++,createprocess,C++,Createprocess,我正在使用这段代码从GUI应用程序启动一个进程。但是,根据这段代码的注释,不可能从控制台应用程序启动进程。实际上,我想这样做,我想一个控制台应用程序启动另一个控制台进程,请你有任何想法如何做到这一点 // This technique must be used for "console-less" parents such as GUI // applications or detached applications. // Using the STARTUPINFO STARTF_USEST

我正在使用这段代码从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) */ 
你试过了吗?我认为这很有效。

您可以尝试: ShellExecute(),ShellExecuteEx(),CreateProcess(),system(),_wsystem()

还有一些,但其中一个对你有用

就我个人而言,我会使用CreateProcess,然后等待进程退出(在google上找到这个例子:)。请注意,system()/_wsystem()是最容易使用的,但如果您不小心,它们可能会被利用

希望有帮助!:-)

请尝试以下代码:

#include <Windows.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{

    char* app_to_launch=new char[80];
    strcpy(app_to_launch,"app.exe");

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );


    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        app_to_launch,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );

    }


    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

    return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
char*app_to_launch=新字符[80];
strcpy(app-to-launch,“app.exe”);
STARTUPINFO si;
处理信息;
零内存(&si,sizeof(si));
si.cb=sizeof(si);
零内存(&pi,sizeof(pi));
//启动子进程。
如果(!CreateProcess(NULL,//没有模块名称(使用命令行)
app-to-launch,//命令行
NULL,//进程句柄不可继承
NULL,//线程句柄不可继承
FALSE,//将句柄继承设置为FALSE
0,//没有创建标志
NULL,//使用父级的环境块
NULL,//使用父级的起始目录
&si,//指向STARTUPINFO结构的指针
&pi)//指向进程信息结构的指针
) 
{
printf(“CreateProcess失败(%d)。\n”,GetLastError());
}
//等待子进程退出。
WaitForSingleObject(pi.hProcess,无限);
//关闭进程和线程句柄。
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
返回0;
}

从控制台应用程序(或任何其他应用程序)创建子进程非常简单。在继承stdout、stdin和stderr重定向的句柄时执行此操作完全是另一个问题,并且似乎是这段代码的要点(和错误)。你可能想先了解一下。嗨,克雷格,谢谢你的回复,但是你有一段代码如何从控制台应用程序调用启动子进程吗?。谢谢您有一段启动子进程的代码。您没有的是一段代码,它以您希望的方式正确设置句柄继承。关于这一点,我建议您参考我之前的评论。您好,Graig,我理解,但是,您能否提供一些参考信息,说明如何设置此代码以从控制台应用程序启动流程?也许可以将该应用程序免费提供给启动ptr(或者使其自动)。