Why does RegOpenKeyExA
throw a path not found error according to the error codes from Microsoft docs
other paths (no spaces) do open flawlessly
int res;
HKEY hKey;
res = RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE\\Policies\\Microsoft\\Windows Defender",
0, KEY_QUERY_VALUE|KEY_WRITE|KEY_READ|KEY_SET_VALUE, &hKey);
std::cout << " Error code ["+res+"]"<<std::endl;
the error code I am getting is 2
I am running the compiled executable as admin
RegOpenKeyEx()
allows spaces just fine. MANY Registry keys have spaces in their names.
Error 2 is ERROR_FILE_NOT_FOUND
, which means the key you are trying to open does not exist. And indeed, the Windows Defender
key does not exist in HKEY_CURRENT_USER
, it exists in HKEY_LOCAL_MACHINE
instead.
BTW, KEY_WRITE
includes KEY_SET_VALUE
(among others), and KEY_READ
includes KEY_QUERY_VALUE
(among others), so technically you are opening the key with just KEY_WRITE|KEY_READ
. Which is likely to fail unless your program is running with elevated permissions, since most of HKEY_LOCAL_MACHINE
is read-only to non-admin users. It is rarely of good idea to open a key under HKEY_LOCAL_MACHINE
for both read and write permissions at the same time. If you need to read something, open the key for read-only access, which is likely to succeed. If you need to write something, open the key for write-only access, which will succeed only if you have permission to write.