Registry Set Value
A minimal C program that opens a registry key and writes a string value with RegSetValueEx.
The shortest complete example of writing a registry value. Open the target key with RegOpenKeyEx requesting KEY_ALL_ACCESS, then call RegSetValueEx with the value name, data type (REG_SZ here), and the byte buffer - note the length is strlen(Data), not including the null terminator, which is the usual convention for REG_SZ. Pairs naturally with the Registry Delete Key article as the write half of basic registry maintenance.
#include <Windows.H>
#include <Stdio.H>
int main()
{
HKEY key = 0;
char *Data = "Value Data";
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT,"*", 0, KEY_ALL_ACCESS, &key))
{
RegSetValueEx(key,"Value Name", 0,REG_SZ,(unsigned char*)Data,strlen(Data));
RegCloseKey(key);
}
return 0;
}
If the parent key does not exist yet, create it first with RegCreateKeyEx; for other data types swap REG_SZ for REG_DWORD, REG_BINARY, etc., and adjust the length accordingly (a REG_DWORD takes sizeof(DWORD) bytes).