Access denied for registries

I play the registry programmatically for the first time, and it does not work so well (but at least I did not destroy my computer). In particular, I keep returning Error 5 (Access Denated) from RegCreateKeyEx and RegSetValueEx. The strangest thing for me is that when it HKEY_CURRENT_USER\Software\dir1\Sub Directoryalready exists, RegCreateKeyEx fails with error 5, but when it does not exist, it creates it successfully; and then crash in RegSetValueEx.

Am I doing something wrong in this code?

BOOL MyDialog::SaveLocationsToRegistry()
{
    HKEY   hkey;
    DWORD  dwDisposition;
    DWORD dwType, dwSize;
    LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"), 
                                 0, NULL, 0, 0, NULL, &hkey, &dwDisposition);
    if(result == ERROR_SUCCESS)
    {
        LPCTSTR szLastFolder = "C:\\Documents and Settings\\user\\My Documents\\Copy of item\0";
        dwType = REG_SZ;
        dwSize = strlen(szLastFolder)+1;
        LONG setResult = RegSetValueEx(hkey, TEXT("LastFolder"), 0, dwType, 
        (PBYTE)&szLastFolder, dwSize);
        RegCloseKey(hkey);
        return setResult == ERROR_SUCCESS;
    }
    else
    {
        return false;
    }
}

Note. The absolute path exists only temporarily. Children's steps; -)

+3
source share
1 answer

. , KEY_WRITE ( -) 6- (samDesired).

LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
                 0, NULL, 0, KEY_WRITE, NULL, &hkey, &dwDisposition);
+15

Source: https://habr.com/ru/post/1721760/


All Articles