I've got most of the code for writing a value to the windows registry, however when I change the path to a dummy key and value that I've set up for testing it fails. My code is below:
HKEY hKey; LPCTSTR sk = TEXT("SOFTWARE\TestSoftware"); LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS , &hKey); if (openRes==ERROR_SUCCESS) { printf("Success opening key."); } else { printf("Error opening key."); } LPCTSTR value = TEXT("TestSoftwareKey"); LPCTSTR data = "TestData\0"; LONG setRes = RegSetValueEx (hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data)+1); if (setRes == ERROR_SUCCESS) { printf("Success writing to Registry."); } else { printf("Error writing to Registry."); } LONG closeOut = RegCloseKey(hKey); if (closeOut == ERROR_SUCCESS) { printf("Success closing key."); } else { printf("Error closing key."); }
All three tests yield error statuses.
The part that confuses me is that I was able to run this code when pointing it at other portions of the registry. Any ideas?
thanks, brian
Creating a string value is similar to creating a key – right-click on the key where you want to create the string value, hover on New in the menu, and then select String Value from the submenu.
String Value: String Values are mostly human readable and only have a single line of textual information, like file paths. String Values are one of the most commonly used values in the Windows Registry. Binary Value: As the name implies, these values only contain binary data (0, 1).
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.
To change a value, double-click it in the value pane, or select it and then click Modify on the Edit menu. When you open a value, Regedit displays a different dialog box, depending on the value's type.
I feel silly. The solution is that need to properly escape the slash in the string as follows:
LPCTSTR sk = TEXT("SOFTWARE\\TestSoftware");
Hopefully someone finds this useful...
HKEY OpenKey(HKEY hRootKey, char* strKey) { HKEY hKey; LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey); if (nError==ERROR_FILE_NOT_FOUND) { cout << "Creating registry key: " << strKey << endl; nError = RegCreateKeyEx(hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL); } if (nError) cout << "Error: " << nError << " Could not find or create " << strKey << endl; return hKey; } void SetintVal(HKEY hKey, LPCTSTR lpValue, DWORD data) { LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); if (nError) cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl; } DWORD GetintVal(HKEY hKey, LPCTSTR lpValue) { DWORD data; DWORD size = sizeof(data); DWORD type = REG_DWORD; LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size); if (nError==ERROR_FILE_NOT_FOUND) data = 0; SetVal() is called. else if (nError) cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl; return data; } BOOL SetcharVal(HKEY Key,char* subkey,char* StringName,char* Stringdata) { HKEY hKey = OpenKey(Key,subkey); LONG openRes = RegOpenKeyEx(Key, subkey, 0, KEY_ALL_ACCESS , &hKey); if (openRes==ERROR_SUCCESS) { } else { printf("Error opening key."); } LONG setRes = RegSetValueEx (hKey, StringName, 0, REG_SZ, (LPBYTE)Stringdata, strlen(Stringdata)+1); if (setRes == ERROR_SUCCESS) { } else { printf("Error writing to Registry."); } LONG closeOut = RegCloseKey(hKey); if (closeOut == ERROR_SUCCESS) { } else { printf("Error closing key."); } } char* GetCharVal(HKEY Key,char* subkey,char* StringName) { DWORD dwType = REG_SZ; HKEY hKey = 0; char value[1024]; DWORD value_length = 1024; RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey); RegQueryValueEx(hKey, StringName, NULL, &dwType, (LPBYTE)&value, &value_length); RegCloseKey(hKey); return value; }
I am using this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With