我正在尝试关闭我正在创建的进程的句柄。我开始创建它:
PROCESS_INFORMATION pi;
STARTUPINFOA si = {sizeof(si)};
char szAppPath[MAX_PATH] = "";
GetModuleFileNameA(NULL, szAppPath, MAX_PATH);
std::string strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\")) + "\\game.exe";
int creation = CreateProcessA(strAppDirectory.c_str(), NULL,
NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL,
strAppDirectory.substr(0, strAppDirectory.rfind("\\")).c_str(),
&si, &pi);
然后我检查进程是否像这样运行:
if (creation != 0 && IsProcessRunning(pi.dwProcessId))
{
cout << "created process is running\n";
ResumeThread(pi.hThread);
WaitForInputIdle(pi.hProcess, INFINITE);
cout << pi.dwProcessId << "\n";
SystemHandle* handle = SystemHandle::EnumerateProcessHandles(pi.dwProcessId);
cout << handle << "\n\n";
if (handle != NULL)
{
NTSTATUS cExecution = handle->Close(true);
if (NT_SUCCESS(cExecution))
{
cout << "success\n";
}
else
{
cout << "failure\n";
}
}
free(&handle);
handle = NULL;
}
在其他计算机上,我们会收到错误:
game.exe中0x00407A54的第一次机会异常:0xC0000005:访问冲突读取位置0x00003102。
和
game.exe中0x00407A54处的未处理异常:0xC0000005:访问冲突读取位置0x00003102。
在我的机器上,这种情况每次都有效,但在其他情况下,它们经常会崩溃。如果我发表评论WaitForInputIdle(pi.hProcess, INFINITE);
,我可以重现这次崩溃,这让我想到:是因为他们的计算机速度较慢?
我在崩溃发生时休息一下,它告诉我这个要点(SystemHandle.cpp)中的第30行导致了这些问题: https://gist.github.com/anonymous/6a8cfb1fd00bd9525818