65.9K
CodeProject is changing. Read more.
Home

Reading value from a key in Windows Registry (VC++ code)

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (3 votes)

Oct 31, 2011

CPOL
viewsIcon

32890

How to read a value from a key in the Windows Registry.

	wchar_t buffer[MAX_PATH];
	wchar_t* szValueName;
	wchar_t* SubKeyName;
	wchar_t* installPath;
	HKEY hKey;
	DWORD dwBufLen;
	LONG lret;
 
	HRESULT hr = E_FAIL;
	DWORD* reservedNULL = NULL;
	DWORD reservedZero = 0;
 
	szValueName = L"Path";
	SubKeyName = L"SOFTWARE\\7-Zip";
	lret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
			  SubKeyName,
			  reservedZero,
			  KEY_READ,
			  &hKey);
	if (lret == ERROR_SUCCESS)
	{
		dwBufLen = MAX_PATH;
		lret = RegQueryValueEx(hKey,
				  szValueName,
				  reservedNULL,
				  NULL,
				  (BYTE*)buffer,
				  &dwBufLen);
		if (lret == ERROR_SUCCESS)
		{
		   installPath = buffer;
		   hr = S_OK;
		} // end if successfull value read
		RegCloseKey(hKey);
	} // end if successfule key open