Save string to Windows registry in C++/MFC

The following function saves a string in Windows registry. Coded in C++/MFC. The advantage here is that it creates the key if it doesn’t exist.

void SaveStringToRegistry(HKEY hKey, CString szSubkey,CString szValueName,CString szData)
	HKEY retKey;
	CString szFull = szSubkey;
    LONG retval = RegCreateKey(hKey,szFull, &retKey);
	if (retval == ERROR_SUCCESS) 
		RegSetValueEx(retKey,szValueName,0, REG_SZ,(const BYTE *)LPCTSTR(szData),(szData.GetLength()+1)*sizeof(TCHAR));

Leave a Reply

Your email address will not be published. Required fields are marked *