gpt4 book ai didi

c++ - WaitForSingleObject 不等待进程结束

转载 作者:行者123 更新时间:2023-12-03 12:48:10 31 4
gpt4 key购买 nike

我想等待进程执行(calc.exe)结束,但它不起作用。我的程序很快完成,而我的进程(calc.exe)继续运行(我没有停止它)。

WaitForSingleObject 立即返回 WAIT_OBJECT_0。

ps:我禁用了我的防病毒软件 (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;
}

最佳答案

我发现了错误。 “calc.exe”在退出之前创建另一个进程。我在主“Sleep(60 * 1000);”中使用 1 行代码创建/调用了自己的程序。现在好了:)

关于c++ - WaitForSingleObject 不等待进程结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51674939/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com