-1

I want to get the path to the Desktop from the registry, but it returns %SRRFL%DstpE%\Desktop even though the value in registry is set to %USERPROFILE%\Desktop. Why does this happen?

Here is my code:

string desktop_path;
HKEY hKey;

LONG error = RegOpenKeyExA(
    HKEY_CURRENT_USER,
    "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
    NULL,
    KEY_WOW64_64KEY | KEY_QUERY_VALUE,
    &hKey
);
if (error == ERROR_SUCCESS) {
    wchar_t buffer[MAX_PATH];
    DWORD bufferSize = MAX_PATH;
    LONG result = RegQueryValueExA(
        hKey,
        "Desktop",
        NULL,
        NULL,
        (LPBYTE)&buffer,
        &bufferSize);

    if (result == ERROR_SUCCESS) {
        wstring ws(buffer);
        string str(ws.begin(), ws.end());

        cout << "The reg key is: " << str<< endl;
        desktop_path = str;
    }
}
Adrian W
  • 4,563
  • 11
  • 38
  • 52
k1nd
  • 5
  • 1
  • 3
    Does this answer your question? [How to read a value from the Windows registry](https://stackoverflow.com/questions/34065/how-to-read-a-value-from-the-windows-registry) – Charly May 29 '21 at 10:17
  • 1
    Use SHGetKnownFolderPath instead of reading an undocumented registry key. Perhaps you didn't notice the message in the registry key telling you not to use it – Raymond Chen May 29 '21 at 20:31

1 Answers1

-1

Seeing that you need to get the output in form of std::string, you don't have to go through all those type conversions from wide char to wide string to string

I replaced "wchar_t buffer[MAX_PATH];" with "char buffer[MAX_PATH];" and replaced the string conversions with "std::string str(buffer);"

This worked and gave the output properly.enter image description here

Bharath Suresh
  • 483
  • 2
  • 18
  • Documentation reference supporting this answer: https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types "`REG_SZ` A null-terminated string. **This will be either a Unicode or an ANSI string, depending on whether you use the Unicode or ANSI functions.**" – Ben Voigt May 31 '21 at 15:15