Get string from Windows registry in C++/MFC



Go to get string from Windows registry. Coded in C++/MFC.

 
CString RetrieveStringFromRegistry(HKEY hKey, CString szSubkey,CString szValueName,CString szDefault)
{
 
	HKEY retKey;
	TCHAR retdata[1024];
 
	retdata[0] = 0;
 
	CString szFull = szSubkey;
 
	LONG retval = RegOpenKeyEx(hKey,szFull, 0,KEY_READ, &retKey);
 
    if (retval == ERROR_SUCCESS) {
 
		//since this is in bytes we need to multiply by size of TCHAR
		DWORD datasize = 1024*sizeof(TCHAR);
		DWORD dwType;
 
		RegQueryValueEx(retKey,szValueName,NULL,&dwType, (BYTE *) retdata, &datasize);
 
		RegCloseKey(hKey);
		RegCloseKey(retKey);
 
	}else {
		return szDefault;
	}
 
 
	return CString(retdata);
}


Leave a Reply

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