WaitForSingleObject doesn't wait the end of the process [duplicate]

五迷三道 提交于 2019-12-20 07:47:09

问题


I would like to wait the end of the process execution (calc.exe) but it doesn't work. My program finishes fast/now while my process(calc.exe) continue to run (I don't stopped it).

and WaitForSingleObject returns immediatly WAIT_OBJECT_0.

ps: I disabled my software anti-virus (AVIRA)

int main(int argc, char** arv)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

const char * calcPrgm = "C:\\\\Windows\\System32\\calc.exe";
LPSTR calcPrgmLpstr = const_cast<LPSTR>(calcPrgm);

// Start the child process. 
if (!CreateProcess(NULL,   // No module name (use command line)
    calcPrgmLpstr,        // 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());
    return -1;
}

// Wait until child process exits.
auto ret = WaitForSingleObject(pi.hProcess, INFINITE);
printf("WaitForSingleObject ret = %x\n", ret);
if (ret == WAIT_OBJECT_0)
{
    printf("WaitForSingleObject ret ret == WAIT_OBJECT_0\n");
}
BOOL b = FALSE;
DWORD n = 0;
b = GetExitCodeProcess(pi.hProcess, &n);

// Close process and thread handles. 
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
printf("WaitForSingleObject end\n");
return 0;
}

回答1:


I found the error. "calc.exe" create another process before to exit. I created/called my own program with 1 line of code in the main "Sleep(60 * 1000);". It's ok now :)



来源:https://stackoverflow.com/questions/51674939/waitforsingleobject-doesnt-wait-the-end-of-the-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!