To safely determine the existence of a registry key, use the RegOpenKeyExW function with the KEY_READ flag. If the function returns ERROR_SUCCESS, the key exists; if it returns ERROR_FILE_NOT_FOUND, the key does not exist.
To programmatically get the value of a registry key, use the following APIs:
These functions take the key handle and the name of the value to retrieve as parameters. They return the value in the provided reference parameter.
The following sample code demonstrates the usage of these functions:
<code class="cpp">#include <Windows.h> int main() { HKEY hKey; LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\Perl", 0, KEY_READ, &hKey); if (lRes == ERROR_SUCCESS) { std::wstring strBinDir; GetStringRegKey(hKey, L"BinDir", strBinDir, L"bad"); DWORD dwValue; GetDWORDRegKey(hKey, L"PerlVersion", dwValue, 0); bool bEnabled; GetBoolRegKey(hKey, L"Enabled", bEnabled, false); } RegCloseKey(hKey); return 0; }</code>
In this example, the GetStringRegKey function retrieves the value of the "BinDir" string key, GetDWORDRegKey retrieves the value of the "PerlVersion" DWORD key, and GetBoolRegKey retrieves the value of the "Enabled" Boolean key.
The above is the detailed content of How Can I Retrieve and Determine the Existence of Registry Keys in Windows?. For more information, please follow other related articles on the PHP Chinese website!