要安全地检查注册表项是否存在,请将 RegOpenKeyExW 与 KEY_READ 结合使用访问标志。如果函数返回 ERROR_SUCCESS,则该键存在。
<code class="pseudo-code">if (key exists) { get default value of key get string value of key get DWORD value of key }</code>
<code class="cpp">#include <windows.h> int main() { // Open the registry key HKEY hKey; LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\Perl", 0, KEY_READ, &hKey); // Check if the key exists bool bExistsAndSuccess = (lRes == ERROR_SUCCESS); // Get the default value of the key std::wstring strKeyDefaultValue; GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad"); return 0; } // Function to get a string value from the registry 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; }</code>
以上是如何在 C 中检索注册表项和值?的详细内容。更多信息请关注PHP中文网其他相关文章!