Home > other >  RegOpenKeyEx Not finding an existing value
RegOpenKeyEx Not finding an existing value

Time:06-01

I am checking to see if the value "XXS" exists in the run registry

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\XXS", 0, KEY_READ | KEY_WOW64_64KEY, &hkey) == ERROR_FILE_NOT_FOUND)
{
        std::cout << "KEY NOT FOUND";
        LONG createStatus = RegCreateKey(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &hkey); //Creates a key       
        LONG status = RegSetValueEx(hkey, L"XXS", 0, REG_SZ, (LPBYTE)path, (size   1) * sizeof(WCHAR));
}

I am not sure why this is happening and if I am using this function right. The key does exist but it says it does not. The program is 64 bit running on 64 bit pc

CodePudding user response:

I think the issue here is that you do not differentiate between keys and values.

Registry keys are container objects similar to folders. Registry values are non-container objects similar to files. Keys may contain values and subkeys.

So you basically need to open/create a key and then handle the values in it.

In your case, i'd try something like this (but be aware, from the top of my head, not tested):

#define HKLM_BASE L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
HKEY  hKey;
wchar_t buf[256] = { 0 };

LONG  regError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,HKLM_BASE,0, KEY_WOW64_64KEY | KEY_ALL_ACCESS,&hKey);
if (regError == ERROR_SUCCESS)
{
    DWORD dwDataSize = sizeof(buf);
    regError   = RegQueryValueEx(hKey,L"XXS", NULL, NULL,(LPBYTE)&buf,&dwDataSize);
    if ((regError != ERROR_SUCCESS) )
    {
        wchar_t *path  =L"your value";
        if (ERROR_SUCCESS != RegSetValueEx(hKey,L"XXS",NULL,REG_SZ,((const BYTE*)&path),wcslen (path)))
        {
            /* handle error */
        }
    } else {
     /* current value in buf */
    }
    RegFlushKey( hKey );
    RegCloseKey( hKey );
} else {
    /* handle error */
}

CodePudding user response:

Welcome to stack overflow! While typing this I see Morphine was just a little bit quicker. But he forgot to explain most of the the changes he made. So here are my 2 cents. The example of Morphine is fine , I won't provide another one.

I think XXS is a value (since you create a value, not a key, if it does not exist). This means you need to check if the value exist, right after you open the registry key. I also advise you to check on any error returning from the function RegOpenKeyEx, not only the ERROR_FILE_NOT_FOUND error.

Another thing I recommend is using RegCreateKeyEx instead of RegCreateKey, this since the documentation says the following:

Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegCreateKeyEx function. However, applications that back up or restore system state including system files and registry hives should use the Volume Shadow Copy Service instead of the registry functions.

Last but not least, don't forget to close the keys once you do not need them anymore. You don't have to close values.

  • Related