2012-09-20 272 views
0

我正在使用這段代碼從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) */ 
+0

創建從一個控制檯應用程序(或任何其他應用程序)一個子進程是微不足道的。這樣做同時繼承stdout,stdin和stderr重定向的句柄完全是另一個問題,並且似乎是此代碼的要點(和錯誤)。你可能想先閱讀。 – WhozCraig

+0

嗨克雷格,謝謝你的回覆,但你有一段代碼如何調用從控制檯應用程序啓動子進程?謝謝! – user1681210

+0

*您*有一段代碼啓動子進程。你*沒有*是一段代碼,它以你想要的方式設置句柄繼承。爲此,我將你轉到我的事先評論。 – WhozCraig

回答

0

您可以嘗試: ShellExecute(),ShellExecuteEx(),CreateProcess(),system(),_wsystem()。

還有一些,但其中一個爲你工作!

個人而言,我會用CreateProcess去等待進程退出(在谷歌上找到這個例子:http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result)。注意system()/ _ wsystem()是最容易使用的,但是如果你不小心它們可以被利用!

希望它有幫助! :-)

0

試試這個代碼:

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

也許免費* app_to_launch * ptr(或者只是自動)。 – WhozCraig

相關問題