Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VisualC++

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

2.33/5 (3 votes)
22 Nov 2011CPOL 32.9K  
How to read a value from a key in the Windows Registry.
C++
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)