2017-07-20 7 views
0

私は2つのアプリケーション(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; 
} 

第1(SandBox.exe)は、いくつかの引数を持つ最初の実行:からclient.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; 
} 

SandBox.exe、出力は非常に奇妙で、client.exeは決して終了せず、デッドロックのままです。

enter image description here

ここでの問題は何ですか?

私は(私は孤立client.exeを実行したときのような)のようないくつかの出力を期待していた。

enter image description here

+0

デッドロックであることをどのように知っていますか? –

+2

'CreateProcess'の2番目のパラメータは、変更可能な文字列AFAIKを指していなければなりません。 – molbdnilo

+3

'main()'でハンドルを閉じる前に 'WaitForSingleObject(pi.hProcess、INFINITE);'を使ってクライアントプロセスの終了を待つとどうなりますか? – 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); 
関連する問題