Createprocess db2cmd时无法获得主进程句柄导致WaitForSingleObject立即返回

创建进程时下列代码的WaitForSingleObject总是立即返回,不会等待db2cmd窗口关闭后返回。
而使用cmd时则不会有这种问题,猜测是db2cmd额外创建了线程而主线程立即关闭了,不知道有未有同道遇到过一样的问题。
或者有无其他可以在创建另外一个线程时在该线程关闭之前阻塞主线程的方法。

if (!CreateProcess(NULL,   // No module name (use command line)
        "db2cmd",        // 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
        )
    {
        AfxMessageBox("CreateProcess failed (%d).\n", GetLastError());
        return FALSE;
    }

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