milad
milad

Reputation: 21

How Read String Value from Registry in c++?

Please some one tell me whats wrong ?
I copy this code and gives me error ERROR_FILE_NOT_FOUND there is a string value in registry with name of "name"

string GetRegistry()
{
    DWORD dwType = REG_SZ;
    HKEY Handler = 0;
    string subkey = "Key123\\";
    string value_name = "name";
    DWORD AnswerType;
    char data[32];
    DWORD buffer_size = 32;
    LSTATUS Answer = RegOpenKeyA(HKEY_CURRENT_USER, subkey.c_str(), &Handler);
    if (Answer != ERROR_SUCCESS) {
        return "Faild To Open Registry Key";
    }

    Answer=RegGetValueA(Handler, subkey.c_str(), value_name.c_str(), RRF_RT_REG_SZ, &AnswerType, data, &buffer_size);
    RegCloseKey(Handler);
    string return_data = "";
    for (unsigned int a = 0; a < 32; a++) {
        return_data += (char)*(data + a);
    }
    return return_data;
}

Upvotes: 1

Views: 326

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598309

You are misusing RegGetValueA().

Its lpValue parameter is relative to its lpSubKey parameter, which is relative to its hkey parameter.

You are using RegOpenKeyA() (which is deprecated, you should be using RegOpenKeyExA() instead) to open the HKEY_CURRENT_USER\Key123 key, and then you are calling RegGetValuA() with its hKey parameter set to that open key, its lpSubKey parameter set to "Key123", and its lpValue parameter set to "name".

So, you are asking RegGetValueA() to read the HKEY_CURRENT_USER\Key123\Key123\name value, but you meant to read the HKEY_CURRENT_USER\Key123\name value instead. That is why RegGetValueA() is failing with ERROR_FILE_NOT_FOUND.

So, you need to either:

  • use RegOpenKeyExA() to open HKEY_CURRENT_USER\Key123, and then use RegQueryValueEx() (or RegGetValueA() with lpSubKey=NULL) to read the name value in that open key, eg:

    string GetRegistry()
    {
        HKEY Handler = NULL;
        string subkey = "Key123";
        string value_name = "name";
        char data[32];
        DWORD buffer_size = 32;
    
        LSTATUS Answer = RegOpenKeyExA(HKEY_CURRENT_USER, subkey.c_str(), 0, KEY_QUERY_VALUE, &Handler);
        if (Answer != ERROR_SUCCESS) {
            return "Failed To Open Registry Key";
        }
    
        Answer = RegQueryValueExA(Handler, value_name.c_str(), NULL, NULL, (LPBYTE)data, &buffer_size);
        RegCloseKey(Handler);
    
        if (Answer != ERROR_SUCCESS) {
            return "Failed To Read Registry Value";
        }
    
        // the data MAY or MAY NOT be null-terminated,
        // ignore the terminator if it is present...
        if ((buffer_size > 0) && (data[buffer_size-1] == '\0')) {
            --buffer_size;
        }
    
        return string(data, buffer_size);
    }
    
    string GetRegistry()
    {
        HKEY Handler = NULL;
        string subkey = "Key123";
        string value_name = "name";
        char data[32];
        DWORD buffer_size = 32;
    
        LSTATUS Answer = RegOpenKeyExA(HKEY_CURRENT_USER, subkey.c_str(), 0, KEY_QUERY_VALUE, &Handler);
        if (Answer != ERROR_SUCCESS) {
            return "Failed To Open Registry Key";
        }
    
        Answer = RegGetValueA(Handler, NULL, value_name.c_str(), RRF_RT_REG_SZ, NULL, data, &buffer_size);
        RegCloseKey(Handler);
    
        if (Answer != ERROR_SUCCESS) {
            return "Failed To Read Registry Value";
        }
    
        // the data IS null-terminated, ignore the terminator...
        return string(data, buffer_size-1);
    }
    
  • use RegGetValueA() by itself, do not use RegOpenKeyExA() at all, eg:

    string GetRegistry()
    {
        HKEY Handler = NULL;
        string subkey = "Key123";
        string value_name = "name";
        char data[32];
        DWORD buffer_size = 32;
    
        LSTATUS Answer = RegGetValueA(HKEY_CURRENT_USER, subkey.c_str(), value_name.c_str(), RRF_RT_REG_SZ, NULL, data, &buffer_size);
        if (Answer != ERROR_SUCCESS) {
            return "Failed To Read Registry Key Value";
        }
    
        // the data is null-terminated, ignore the terminator...
        return string(data, buffer_size-1);
    }
    

Upvotes: 2

Related Questions