Get integer from Windows registry in C++/MFC

Following code gets integer from Windows registry using C++/MFC.

UINT RetrieveUIntFromRegistry(HKEY hKey, CString szSubkey,CString szValueName,UINT iDefault)
	HKEY retKey;
	UINT retdata;
	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 = sizeof(UINT);
		DWORD dwType;
		if (ERROR_SUCCESS  != RegQueryValueEx(retKey,szValueName,NULL,&dwType, (BYTE *) &retdata, &datasize))
			retdata = iDefault;
	}else {
		return iDefault;
	return retdata;

Leave a Reply

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