【问题标题】:Launch console app from console apps从控制台应用程序启动控制台应用程序
【发布时间】:2012-09-20 17:01:47
【问题描述】:

我正在使用这段代码从 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 重定向句柄的同时这样做完全是另一个问题,并且似乎是这段代码的重点(和错误)。你可能想先阅读一下。
  • 嗨 Craig,感谢您的回复,但是您有一段代码如何调用从控制台应用程序启动子进程吗?谢谢!
  • 有一段代码可以启动一个子进程。您没有的是一段代码,它以您想要的方式正确设置句柄继承。对此,我建议您参考我之前的评论。
  • 嗨 Graig,我明白了,但是,您能否提供一些参考,如何设置此代码以从控制台应用程序启动进程?

标签: c++ createprocess


【解决方案1】:

你试过shellexecute吗?我认为这行得通..

【讨论】:

    【解决方案2】:

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

    还有一些,但其中之一必须为您工作!

    就个人而言,我会选择 CreateProcess,然后等待进程退出(在谷歌上找到这个例子:http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result)。注意 system()/_wsystem() 是最容易使用的,但如果你不小心,它们可能会被利用!!!

    希望对您有所帮助! :-)

    【讨论】:

      【解决方案3】:

      试试这个代码:

      #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;
      }
      

      【讨论】:

      • 也许可以免费使用 app_to_launch ptr(或者只是让它自动)。
      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2017-05-21
      • 2011-01-22
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多