views:

2100

answers:

4

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.

A: 

RegQueryValueEx

This gives the value if it exists, and returns an error code ERROR_FILE_NOT_FOUND if the key doesn't exist.

(I can't tell if my link is working or not, but if you just google for "RegQueryValueEx" the first hit is the msdn documentation.)

Tyler
A: 

The pair RegOpenKey and RegQueryKeyEx will do the trick.

If you use MFC CRegKey class is even more easier solution.

Serge
+2  A: 
const CString REG_SW_GROUP_I_WANT = _T("SOFTWARE\\My Corporation\\My Package\\Group I want");
const CString REG_KEY_I_WANT= _T("Key Name");

   CRegKey regKey;
   DWORD   dwValue = 0;

   if(ERROR_SUCCESS != regKey.Open(HKEY_LOCAL_MACHINE, REG_SW_GROUP_I_WANT))
   {
      m_pobLogger->LogError(_T("CRegKey::Open failed in Method"));
      regKey.Close();
      goto Function_Exit;
   }
   if( ERROR_SUCCESS != regKey.QueryValue( dwValue, REG_KEY_I_WANT))
   {
      m_pobLogger->LogError(_T("CRegKey::QueryValue Failed in Method"));
      regKey.Close();
      goto Function_Exit;
   }

   // dwValue has the stuff now - use for further processing
Gishu
+4  A: 

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;
}
Brian R. Bondy