Search code examples
c++visual-studio-2010visual-c++registry

Adding string to registry path regopenkeyex


Hey so let's say this is the code :

HKEY hk;
long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,L"SOFTWARE\\",
                  0,KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hk );
if ( n == ERROR_SUCCESS ) {
    cout << "Found ! " << endl;
}
else {
    cout << "Failed with value " << n << endl;
}
RegCloseKey(hk);

So I have a variable string that i need to add to the path that might looks like this :

 string s = "test";

How to make L"SOFTWARE\\" + s as a path?

Thank you


Solution

  • I assume you tried:

    long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\" + s, ... );
    

    Which probably gave you a compiler warning. Since you need to add a variable string to the path, you can use:

    const std::string sFullPath = "SOFTWARE\\" + s;
    long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sFullPath.c_str(), 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hk);
    

    This works because RegOpenKeyEx takes a const char* as its argument.