Search code examples
c++visual-studioregistry

RegSetValueEx inputting incorrect data into Registry


The issue I'm having is that my application is inputting the incorrect data into the registry (instead of a file path, it inputs characters such as "㩃啜敳獲啜敳屲灁") and I don't know what the issue is, the relevant source code is below (Includes, etc have been removed).

stringstream ss;

char* file_path = getenv("APPDATA");
strcat(file_path, "\\Application.exe");
ss << file_path;
ss >> file_path_string;
CA2W unicodeFile_path(file_path);
cout << "Downloading File";
HRESULT hr = URLDownloadToFile ( NULL, _T("http://example.com/application.exe"), unicodeFile_path, 0, NULL );
cout << "Done" << endl;
cout << "Adding to registry" << endl;
HKEY hKey;
CA2W registryLocation("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
CA2W registryKey("Application");
// Check registry if exists, otherwise create.
RegCreateKeyEx(HKEY_CURRENT_USER,
               registryLocation,
               0,
               NULL,
               REG_OPTION_NON_VOLATILE,
               KEY_WRITE,
               NULL,
               &hk,
               &dwDisp);
// Store reg value
RegSetValueEx(hk,
              registryKey,
              NULL,
              REG_SZ,
              (const BYTE*)file_path_string.c_str(),
              file_path_string.size() + 1);

Any help will be appreciated.


Solution

  • RegSetValueEx expects (for REG_SZ) that the data you're passing in is Unicode, unless you're not defining UNICODE in your environment. But .c_str is likely a non-Unicode stream.