Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 22 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mitendra Anand
Architect
India India
My name is Mitendra Anand and my work is focused around application development which includes a lot of prototyping of new solutions.

While I have a background in C++/VC++ programming, my daily work is mostly spent in C++, Sybase, SQL, Unix/Windows.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberluckyrockstar50510-Jul-14 9:53 
GeneralThe method of defining a union RegKeyValue just to get somet... Pinmembernv322-Nov-11 4:46 
The method of defining a union RegKeyValue just to get something of data type BYTE is rather clumsy. You can use any kind of buffer and cast the address in the call to RegQueryValueEx. The parameter is defined as LPBYTE in order to make the caller aware of the fact that the following parameter lpcbData, specifying the length of the buffer, is understood as "length in bytes". So you could equally well write:
 
wchar_t buffer[MAX_PATH];
 
...
 
lret = RegQueryValueEx(hKey,
szValueName,
reservedNULL,
NULL,
(BYTE*) buffer,
&dwBufLen);
 
That said, the example is really trivial.
GeneralRe: Thanks for correcting it. Your code certainly is more neat. ... PinmemberMitendra Anand22-Nov-11 5:26 
GeneralReason for my vote of 1 Trivial PinmemberMember 351467110-Nov-11 4:04 
GeneralRe: Can you provide me a better way to read the reg value from C... PinmemberMitendra Anand10-Nov-11 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 22 Nov 2011
Article Copyright 2011 by Mitendra Anand
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid