2017-07-20 48 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);