Wednesday, August 01, 2007

C++: Set Registry Key Values

This piece of example code writes a key to the windows registry.

#include <windows.h>

int SetRegValue(char *key_name,char *key_word,char *b,int dwType,int s)
{
int j=0;

DWORD lpdwDisposition;
HKEY hKey;

j=RegCreateKeyEx(
HKEY_CURRENT_USER,
key_name,
0, /* reserved */
NULL, /* address of class string */
REG_OPTION_NON_VOLATILE, /* special options flag */
KEY_WRITE, /* desired security access */
NULL, /* address of key security structure */
&
hKey, /* address of buffer for opened handle */
&
lpdwDisposition /* address of disposition value buffer */
);

if (j==ERROR_SUCCESS)
{
RegSetValueEx(hKey, key_word, 0, dwType, (unsigned char *)b, s);
RegCloseKey(hKey);
}
return(j);
}

int main(int argc, char* argv[])
{
char val[100]="myusername";
SetRegValue("Software\\MySoft\\Settings","Username", val, REG_SZ, 100);
return 0;
}



source(s): http://msdn2.microsoft.com/en-us/library/ms724923.aspx
http://msdn2.microsoft.com/en-us/library/ms724875.aspx

No comments: