Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a value from the Windows registry

Given the key for some registry value (e.g. HKEY_LOCAL_MACHINE\blah\blah\blah\foo) how can I:

  1. Safely determine that such a key exists.
  2. Programmatically (i.e. with code) get its value.

I have absolutely no intention of writing anything back to the registry (for the duration of my career if I can help it). So we can skip the lecture about every molecule in my body exploding at the speed of light if I write to the registry incorrectly.

Prefer answers in C++, but mostly just need to know what the special Windows API incantation to get at the value is.

like image 790
nolandda Avatar asked Aug 29 '08 06:08

nolandda


People also ask

How do I read Windows Registry files?

There are two ways to open Registry Editor in Windows 10: In the search box on the taskbar, type regedit, then select Registry Editor (Desktop app) from the results. Right-click Start , then select Run. Type regedit in the Open: box, and then select OK.

How do I read registry keys?

You can use Get-ChildItem to view registry keys and Set-Location to navigate to a key path. Registry values are attributes of a registry key. In the Registry drive, they are called Item Properties. A registry key can have both children keys and item properties.

How do I check my registry value?

Click Start or press the Windows key . In the Start menu, either in the Run box or the Search box, type regedit and press Enter . In Windows 8, you can type regedit on the Start screen and select the regedit option in the search results. In Windows 10, type regedit in the Search box on the taskbar and press Enter .

What is value data in regedit?

A registry value can store data in various formats. When you store data under a registry value, for instance by calling the RegSetValueEx function, you can specify one of the following values to indicate the type of data being stored.


1 Answers

Here is some pseudo-code to retrieve the following:

  1. If a registry key exists
  2. What the default value is for that registry key
  3. What a string value is
  4. What a DWORD value is

Example code:

Include the library dependency: Advapi32.lib

HKEY hKey; LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Perl", 0, KEY_READ, &hKey); bool bExistsAndSuccess (lRes == ERROR_SUCCESS); bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND); std::wstring strValueOfBinDir; std::wstring strKeyDefaultValue; GetStringRegKey(hKey, L"BinDir", strValueOfBinDir, L"bad"); GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad");  LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue) {     nValue = nDefaultValue;     DWORD dwBufferSize(sizeof(DWORD));     DWORD nResult(0);     LONG nError = ::RegQueryValueExW(hKey,         strValueName.c_str(),         0,         NULL,         reinterpret_cast<LPBYTE>(&nResult),         &dwBufferSize);     if (ERROR_SUCCESS == nError)     {         nValue = nResult;     }     return nError; }   LONG GetBoolRegKey(HKEY hKey, const std::wstring &strValueName, bool &bValue, bool bDefaultValue) {     DWORD nDefValue((bDefaultValue) ? 1 : 0);     DWORD nResult(nDefValue);     LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult, nDefValue);     if (ERROR_SUCCESS == nError)     {         bValue = (nResult != 0) ? true : false;     }     return nError; }   LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue) {     strValue = strDefaultValue;     WCHAR szBuffer[512];     DWORD dwBufferSize = sizeof(szBuffer);     ULONG nError;     nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);     if (ERROR_SUCCESS == nError)     {         strValue = szBuffer;     }     return nError; } 
like image 53
Brian R. Bondy Avatar answered Sep 21 '22 10:09

Brian R. Bondy