在作为函数参数的句柄上调用 CloseHandle? [英] Call CloseHandle on handle that is a function parameter?
问题描述
如果一个句柄是作为函数参数创建的,函数结束时它会自己关闭吗?
If a handle is created as a function parameter, will it close itself when the function ends?
例如:
int readMem(HANDLE processHandle, int address)
{
int memValue = 0;
bool success = ReadProcessMemory(processHandle, (LPVOID)address, &memValue, sizeToReadBytes, NULL);
if (!success)
std::wcout << "Memory read failed on address: " << std::hex << address << "\n";
return memValue;
}
我是否需要在 return 语句之前显式关闭它,还是最好尽可能将它们作为引用传递?
Do I need to explicitly close it before the return statement or is it best to just pass them as references whenever possible?
或者我只是误解了 Handles 的全部内容?我对 winapi 很陌生.
Or am I just misunderstanding Handles all together? I am quite new to the winapi.
推荐答案
a HANDLE
只是一个 typedef
到一个 void *
.
a HANDLE
is a just a typedef
to a void *
.
Microsoft 明确规定,当您使用完句柄后,请务必使用 CloseHandle
将其关闭.
Microsoft clearly specifies that when you are finished with the handle, be sure to close it using CloseHandle
.
您的 readMem
函数没有创建句柄,因此在那里关闭它没有意义.
Your readMem
function didn't create the handle, therefore it doesn't makes sense to close it there.
这篇关于在作为函数参数的句柄上调用 CloseHandle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!