Reputation: 1835
If I call RegOpenKeyEx()
, do I have to RegCloseKey()
the key regardless whether RegOpenKeyEx()
succeeded, or only if RegOpenKeyEx()
succeeded?
That is:
lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey);
if(lRes == ERROR_SUCCESS)
{
//doSomething
}
RegCloseKey(hKey);
or:
lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey);
if(lRes == ERROR_SUCCESS)
{
//doSomething
...
RegCloseKey(hKey);
}
What is the behavior in the first case? Is closing an unsuccessfully-opened key safe?
Thanks.
Upvotes: 1
Views: 3226
Reputation: 26259
You should only close it if it opened successfully, otherwise the handle returned in the HKEY will be invalid (probably NULL).
Upvotes: 2