To determine if a registry key exists:
<code class="cpp">LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\Perl", 0, KEY_READ, &hKey); if (lRes == ERROR_SUCCESS) { // Key exists } else if (lRes == ERROR_FILE_NOT_FOUND) { // Key does not exist }</code>
To retrieve the default value of a key:
<code class="cpp">std::wstring strKeyDefaultValue; GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad");</code>
To retrieve a string value:
<code class="cpp">std::wstring strValueOfBinDir; GetStringRegKey(hKey, L"BinDir", strValueOfBinDir, L"bad");</code>
To retrieve a DWORD value:
<code class="cpp">DWORD nValue; LONG nError = GetDWORDRegKey(hKey, L"DWORD_Value_Name", nValue, 0);</code>
To retrieve a Boolean value:
<code class="cpp">bool bValue; LONG nError = GetBoolRegKey(hKey, L"BOOL_Value_Name", bValue, false);</code>
The following library dependencies are required for these functions:
Remember, these functions are for reading values only. Avoid writing to the registry if possible.
The above is the detailed content of How to Safely Read Values from the Windows Registry: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!