user1428522
user1428522

Reputation: 183

To read windows registry using a C++ program on Windows 7

I want perform a couple of basic operations on windows registry. I wrote a small C++ program to read current user key. Below is the code snippet. I am honestly not sure why RegOpenKeyEx() isn't returning ERROR_SUCCESS. Please advice.

#include <Windows.h>

#include <iostream>
using namespace std;

int main(){

    HKEY hkey;

    if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SoftwareDevShed TutorialTest"), 0, KEY_ALL_ACCESS, &hkey) != ERROR_SUCCESS)
        cout<<"Error opening the key"<<endl;
    else 
        cout<<"Success"<<endl;
    system("PAUSE");
    return 0;

}

Upvotes: 1

Views: 4090

Answers (3)

Gaurav Raj
Gaurav Raj

Reputation: 708

#include <Windows.h>
#include <iostream>

int main(int argc, char *argv[])
{
    DWORD dwType;
    char  szVersion[255];
    DWORD dwDataSize = 255;
    memset(szVersion, 0, 255);

    // open the key for reading.
    HKEY hkeyDXVer;
    long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Software Nmae\\", 0, KEY_READ, &hkeyDXVer);

    if(ERROR_SUCCESS == lResult)
    {
       // read the version value
       lResult = RegQueryValueEx(hkeyDXVer, "RegistryValue", NULL, &dwType, (BYTE*)szVersion, &dwDataSize);
       if(ERROR_SUCCESS == lResult)
       {
          std::cout << "Value - " << szVersion << std::endl;
       }
    }

    system("pause");
    return 0;
}

This Code works perfectly on all versions of windows.

Upvotes: 1

Viktor Latypov
Viktor Latypov

Reputation: 14467

The TEXT() macro indicates that the key might be opened using the Unicode version.

Try the

RegOpenKeyExA(HKEY_CURRENT_USER, "<your correct Key name with backslashes>", 0, KEY_ALL_ACCESS, &hkey)

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Where have the backslashes gone here: TEXT("SoftwareDevShed TutorialTest")? Shouldn't that read TEXT("Software\\DevShed Tutorial\\Test")?

Upvotes: 7

Related Questions