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; } }