要安全地檢查註冊表項是否存在,請將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中文網其他相關文章!