2017-07-20 80 views
0

我有兩個應用程序(exe)。帶參數死鎖的C++ CreateProcess

第一個(client.exe)僅僅打印出的參數:

#include <iostream> 

int main(int argc, char** argv) 
{ 
    std::cout << "Have " << argc << " arguments:" << std::endl; 
    for (int i = 0; i < argc; ++i) 
    { 
     std::cout << argv[i] << std::endl; 
    } 

    return 0; 
} 

第二個(SandBox.exe)執行的第一個與一些參數:

#include <iostream> 
#include <Windows.h> 

void startup(LPCTSTR lpApplicationName, LPSTR param) 
{ 
    // additional information 
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 

    // set the size of the structures 
    ZeroMemory(&si, sizeof(si)); 
    si.cb = sizeof(si); 
    ZeroMemory(&pi, sizeof(pi)); 

    // start the program up 
    CreateProcess(lpApplicationName, // the path 
     param,//NULL,//argv[1],  // 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 
    ); 

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

int main() 
{ 
    startup(TEXT("client.exe"), TEXT("client.exe thisIsMyFirstParaMeter AndTheSecond")); 
    return 0; 
} 

通過執行從所述client.exeSandBox.exe,輸出非常奇怪,並且client.exe永遠不會結束並保持在的死鎖狀態。

enter image description here

這裏有什麼問題嗎?

我期待像一些輸出(比如當我運行client.exe隔離):

enter image description here

謝謝

+0

你怎麼知道這是一個僵局? –

+2

「CreateProcess」的第二個參數必須指向一個可變字符串AFAIK。 – molbdnilo

+3

如果您使用'WaitForSingleObject(pi.hProcess,INFINITE)等待客戶端進程結束會發生什麼;'在main()'中關閉句柄之前會發生什麼? – vasek

回答

3

你控制應用程序應該等到客戶機退出。 https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx

// start the program up 
CreateProcess(lpApplicationName, // the path 
    param,   // 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 
); 

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

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