gpt4 book ai didi

c++ - 进程后的 CloseHandle 不存在

转载 作者:行者123 更新时间:2023-12-02 10:18:06 25 4
gpt4 key购买 nike

我做了一个简单的例子。

我只想知道在将 handle 重新附加到新进程之前是否需要关闭 handle ?

DWORD g_dwPid = 0;
HANDLE g_hProcess;
int _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {

while (TRUE) {

DWORD dwPid = GetProcessIdByName(L"explorer.exe");
if (dwPid && dwPid != g_dwPid) {
g_dwPid = dwPid;

g_hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, g_dwPid);
}
else
CloseHandle(g_hProcess);

Sleep(500);
}
return 0;
}

最佳答案

是的,您应该始终关闭 OpenProcess 返回的句柄当你完成它时。

来自 documentation :

When you are finished with the handle, be sure to close it using the CloseHandle function.

关于c++ - 进程后的 CloseHandle 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61262522/

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