1

I tried to add a value to registry in c++, but the program shows error. I have a very simple code, when the program run I got the error: Unable to set registry value value_name (RegSetValueEx failed)

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <Windows.h>

using namespace std;


int main() {


    HKEY key;
    if (RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion"), &key) != ERROR_SUCCESS)
    {
        cout << "unable to open registry";
    }



    if (RegSetValueEx(key, TEXT("value_name"), 0, REG_SZ, (LPBYTE)"value_data", strlen("value_data")*sizeof(char)) != ERROR_SUCCESS)
    {
        RegCloseKey(key);
        cout << "Unable to set registry value value_name";
    }
    else
    {
        cout << "value_name was set" << endl;
    }

}
Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
Jenifer22
  • 13
  • 3
  • 2
    So you didn't bother to check the error code? – chris Aug 12 '14 at 13:20
  • The documentation clearly states what is returned from the function when it fails. – chris Aug 12 '14 at 14:15
  • how can I read this doc? – Jenifer22 Aug 12 '14 at 14:19
  • Just google "RegSetValueEx" and you'll [find it pretty easily](http://msdn.microsoft.com/en-ca/library/windows/desktop/ms724923(v=vs.85).aspx). Then you scroll down to the return value and see *If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.* – chris Aug 12 '14 at 14:22

2 Answers2

1

You need to Run as Administrator, or elevated, to make changes to HKLM. Try either that (you can run Visual Studio as admin to debug via F5) or work under HKEY_CURRENT_USER instead.

  • thank you I changed HKEY_LOCAL_MACHINE to HKEY_CURRENT_USER and now it works but what does it mean HKEY_CURRENT_USER and why HKEY_LOCAL_MACHINE didn't work? – Jenifer22 Aug 12 '14 at 14:18
  • Basically the registry is divided into system-wide tree which is common to all processes running as any user snd that is HKLM, and HKCU which is user-specific. Because writing/modifying/deleting stuff under HKLM might and quite often will damage your system including making your pc unbootable, installing rootkits and so on, full admin rights are needed kind of like writing to program files and system32 folders. HKCU only affects processes running under your user, the effect is less dramatic. Every user of your system has a different HKCU. Hope this helps. – Cristian Niculescu Aug 13 '14 at 00:27
1

There are a few reasons why this might fail:

  1. You need to have administrator rights to modify keys and values under HKLM. You'll need to make sure that your process runs elevated. Typically that means specifying the requireAdministrator option to your manifest.
  2. Your anti-malware software is blocking your attempts to write to this key.

The other factor that might well confuse you is the registry redirector. Once you get the value being written you might not be able to find it. For a 32 bit process running on a 64 bit system, the redirector will store the value in the 32 bit view. That 32 bit view lives under HKLM\Software\Wow6432Node.

When debugging problems like this, it pays to read the documentation carefully regarding error handling. In this case, RegSetValueEx returns a Win32 error code. If that value is not equal to ERROR_SUCCESS then the function call failed. However, the specific value returned indicates why the call failed.

One final point to make is that you should be using RegOpenKeyEx rather than the long deprecated RegOpenKey.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490