C++ 关闭已创建进程的互斥句柄
我试图关闭我正在创建的进程的句柄。我从这样创建它开始:C++ 关闭已创建进程的互斥句柄,c++,mutex,handle,C++,Mutex,Handle,我试图关闭我正在创建的进程的句柄。我从这样创建它开始: PROCESS_INFORMATION pi; STARTUPINFOA si = {sizeof(si)}; char szAppPath[MAX_PATH] = ""; GetModuleFileNameA(NULL, szAppPath, MAX_PATH); std::string strAppDirectory = szAppPath; strAppDirectory = strAppDirectory.substr(0, st
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;
}
然后,我检查流程是否按如下方式运行:
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;
}
if(创建!=0&&IsProcessRunning(pi.dwProcessId))
{
我能不能假设在您将所有cout
行添加到该代码之前是第30行?老实说,我不明白该代码试图实现什么,但调用free()内核句柄上的
显然是错误的。您还在上面的代码中使用它来释放系统句柄
,它是用new
创建的free()
不是通用的“删除某些内容”函数,您可以随意传递任何内容。不,导致崩溃的是cout
。如果它是第一行,第31行也可以。我正在尝试关闭与game.exe关联的互斥句柄,该句柄名为“\\Sessions\\1\\BaseNamedObjects\\GameMutex”
。