Registry

What's wrong with this:

CString newpass = "Hello";
char var2[20];
strcpy(var2, (LPCSTR)newpass); // copies the string

HKEY MyKey;
DWORD dwcbData = sizeof(var2);

RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\WinM",0,KEY_ALL_ACCESS,&MyKey);
RegSetValueEx(MyKey, "WinM", 0, REG_SZ, (LPBYTE)&var2, dwcbData);
RegSaveKey(MyKey, "WinM", NULL);
RegCloseKey(MyKey);
scooter1Asked:
Who is Participating?
 
xLsConnect With a Mentor Commented:
When using RegOpenKeyEx you will open your RootKey, if it does not exist you will get a return value=ERROR_SUCCESS.

To store a key under the RootKey you would use RegSetValueEx.

RegSaveKey will only write the key to specified file, and not to the registry.

Check if your Software\\WinM exist, if not create it, but first get a valid root e.g set to RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software",0,KEY_ALL_ACCESS,&MyKey);

then

RegCreateKeyEx( hKeyRoot, "WinM", 0L, NULL, REG_OPTION_VOLATILE, KEY_READ, NULL, &m_hKey, &dw);

or use RegSetValueEx which adds the key if not existing.

/Thomas

0
 
scooter1Author Commented:
it exist from a previous creation, are you suggesting i dont' need RegSaveKey() ?  and is my conversion of string to char right ?
0
All Courses

From novice to tech pro — start learning today.