I’m working on a C++ function to check if a registry path exists. It handles different root keys (like HKEY_LOCAL_MACHINE
and HKEY_CURRENT_USER
) and uses RegOpenKeyEx()
to open the path.
The following is the function:
BOOL RegistryPathExists(const WCHAR* fullPath) {
HKEY hKeyRoot;
WCHAR keyPath[MAX_PATH];
if (wcsncmp(fullPath, L"HKEY_LOCAL_MACHINE", 18) == 0) {
hKeyRoot = HKEY_LOCAL_MACHINE;
wcscpy_s(keyPath, fullPath + 19);
} else if (wcsncmp(fullPath, L"HKEY_CURRENT_USER", 17) == 0) {
hKeyRoot = HKEY_CURRENT_USER;
wcscpy_s(keyPath, fullPath + 18);
} else {
return false; // Unsupported root key
}
HKEY hKey;
LONG result = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS) {
RegCloseKey(hKey);
return true;
} else {
return false;
}
}
When I pass a path under HKEY_LOCAL_MACHINE
, it works fine, but when I use HKEY_CURRENT_USER
then it fails with error 0x12 (ERROR_NO_MORE_FILES
).
Any idea why it works for HKEY_LOCAL_MACHINE
but fails for HKEY_CURRENT_USER
?
I’m trying to open HKEY_CURRENT_USERSOFTWARETESTVALUE
and HKEY_LOCAL_MACHINESOFTWARETESTVALUE
, and I’ve confirmed that the user account has the necessary permissions to read them by checking the registry.
2
I found that this problem is related to registry-virtualization
And now I can read the path if I change my path to HKEY_USERS(#SID)SOFTWAREAPPpathAPPattributes
And this needs higher privilege (like a admim) .If not,RegOpenKeyEx would return 0x5(ERROR_ACCESS_DENIED).