Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关闭已创建进程的互斥句柄_C++_Mutex_Handle - Fatal编程技术网

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”