WaitForSingleObject no espera el final del proceso [duplicado]
Esta pregunta ya tiene una respuesta aquí:
Por qué esos dos comportamientos diferentes con la función WaitForSingleObject en CPP 2 respuestasMe gustaría esperar el final de la ejecución del proceso (calc.exe) pero no funciona. Mi programa termina rápido / ahora mientras mi proceso (calc.exe) continúa ejecutándose (no lo detuve).
y WaitForSingleObject devuelve inmediatamente WAIT_OBJECT_0.
ps: deshabilité mi antivirus de software (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;
}