」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何安全地從 Windows 登錄讀取值:逐步指南

如何安全地從 Windows 登錄讀取值:逐步指南

發佈於2024-11-03
瀏覽:696

How to Safely Read Values from the Windows Registry: A Step-by-Step Guide

如何安全地從Windows 註冊表讀取值

檢測登錄項目是否存在

確定登錄項目是否存在:

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
}

讀取登錄值

擷取鍵的預設值:

std::wstring strKeyDefaultValue;
GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad");

擷取字串值:

std::wstring strValueOfBinDir;
GetStringRegKey(hKey, L"BinDir", strValueOfBinDir, L"bad");

要檢索 DWORD 值:

DWORD nValue;
LONG nError = GetDWORDRegKey(hKey, L"DWORD_Value_Name", nValue, 0);

要擷取布林值:

bool bValue;
LONG nError = GetBoolRegKey(hKey, L"BOOL_Value_Name", bValue, false);

附加說明

這些函數需要下列函式庫相依性:

  • Advapi32. lib

請記住,這些函數僅用於讀取值。如果可能的話,避免寫入註冊表。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3