如何从Windows注册表中读取值

nol*_*dda 81 c++ windows registry winapi

鉴于某些注册表值的关键(例如HKEY_LOCAL_MACHINE\blah\blah\blah\foo)我该如何:

  1. 安全地确定存在这样的密钥.
  2. 以编程方式(即使用代码)获取其值.

我绝对没有打算将任何东西写回注册表(如果我可以提供帮助的话,我的职业生涯期间).因此,如果我错误地写入注册表,我们可以跳过关于我体内每个分子以光速爆炸的讲座.

首选C++中的答案,但大多只需要知道特殊的Windows API咒语是什么.

Bri*_*ndy 66

这是一些用于检索以下内容的伪代码:

  1. 如果存在注册表项
  2. 该注册表项的默认值是什么
  3. 什么是字符串值
  4. 什么是DWORD值

示例代码:

包含库依赖项:Advapi32.lib

HKEY hKey;
LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Perl", 0, KEY_READ, &hKey);
bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);
std::wstring strValueOfBinDir;
std::wstring strKeyDefaultValue;
GetStringRegKey(hKey, L"BinDir", strValueOfBinDir, L"bad");
GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad");

LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue)
{
    nValue = nDefaultValue;
    DWORD dwBufferSize(sizeof(DWORD));
    DWORD nResult(0);
    LONG nError = ::RegQueryValueExW(hKey,
        strValueName.c_str(),
        0,
        NULL,
        reinterpret_cast<LPBYTE>(&nResult),
        &dwBufferSize);
    if (ERROR_SUCCESS == nError)
    {
        nValue = nResult;
    }
    return nError;
}


LONG GetBoolRegKey(HKEY hKey, const std::wstring &strValueName, bool &bValue, bool bDefaultValue)
{
    DWORD nDefValue((bDefaultValue) ? 1 : 0);
    DWORD nResult(nDefValue);
    LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult, nDefValue);
    if (ERROR_SUCCESS == nError)
    {
        bValue = (nResult != 0) ? true : false;
    }
    return nError;
}


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;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果由于一些莫名其妙的原因,这似乎永远不会找到一个密钥,它可能是一个32位/ 64位的问题.请参见http://stackoverflow.com/q/15084380/482758 (10认同)
  • 这是一个非常晚的评论,但我想提醒所有你应该通过调用RegCloseKey关闭注册表项. (7认同)
  • 可能有助于提及您的代码旨在与Windows调用Unicode字符集一起使用.我将函数调用`RegOpenKeyExW`和`RegQueryValueExW`更改为他们的"字符集"不可知等效`RegOpenKeyEx`和`RegQueryValueEx` (4认同)
  • 这看起来像一个 C API,我们有 2016。将匹配的 Win32 API 包装到 C++ 中,并利用 RAII 获取 HKEY 等资源。 (3认同)

Gis*_*shu 9

const CString REG_SW_GROUP_I_WANT = _T("SOFTWARE\\My Corporation\\My Package\\Group I want");
const CString REG_KEY_I_WANT= _T("Key Name");

CRegKey regKey;
DWORD   dwValue = 0;

if(ERROR_SUCCESS != regKey.Open(HKEY_LOCAL_MACHINE, REG_SW_GROUP_I_WANT))
{
  m_pobLogger->LogError(_T("CRegKey::Open failed in Method"));
  regKey.Close();
  goto Function_Exit;
}
if( ERROR_SUCCESS != regKey.QueryValue( dwValue, REG_KEY_I_WANT))
{
  m_pobLogger->LogError(_T("CRegKey::QueryValue Failed in Method"));
  regKey.Close();
  goto Function_Exit;
}

// dwValue has the stuff now - use for further processing
Run Code Online (Sandbox Code Playgroud)

  • `CRegKey` 听起来像 ATL 或 MFC 国家。 (3认同)

Roi*_*ton 7

从 Windows >=Vista/Server 2008 开始,可以使用RegGetValue,这RegQueryValueEx更安全的功能。无需RegOpenKeyExRegCloseKeyNUL字符串值终止检查(REG_SZREG_MULTI_SZREG_EXPAND_SZ)。

#include <iostream>
#include <string>
#include <exception>
#include <windows.h>

/*! \brief                          Returns a value from HKLM as string.
    \exception  std::runtime_error  Replace with your error handling.
*/
std::wstring GetStringValueFromHKLM(const std::wstring& regSubKey, const std::wstring& regValue)
{
    size_t bufferSize = 0xFFF; // If too small, will be resized down below.
    std::wstring valueBuf; // Contiguous buffer since C++11.
    valueBuf.resize(bufferSize);
    auto cbData = static_cast<DWORD>(bufferSize * sizeof(wchar_t));
    auto rc = RegGetValueW(
        HKEY_LOCAL_MACHINE,
        regSubKey.c_str(),
        regValue.c_str(),
        RRF_RT_REG_SZ,
        nullptr,
        static_cast<void*>(valueBuf.data()),
        &cbData
    );
    while (rc == ERROR_MORE_DATA)
    {
        // Get a buffer that is big enough.
        cbData /= sizeof(wchar_t);
        if (cbData > static_cast<DWORD>(bufferSize))
        {
            bufferSize = static_cast<size_t>(cbData);
        }
        else
        {
            bufferSize *= 2;
            cbData = static_cast<DWORD>(bufferSize * sizeof(wchar_t));
        }
        valueBuf.resize(bufferSize);
        rc = RegGetValueW(
            HKEY_LOCAL_MACHINE,
            regSubKey.c_str(),
            regValue.c_str(),
            RRF_RT_REG_SZ,
            nullptr,
            static_cast<void*>(valueBuf.data()),
            &cbData
        );
    }
    if (rc == ERROR_SUCCESS)
    {
        cbData /= sizeof(wchar_t);
        valueBuf.resize(static_cast<size_t>(cbData - 1)); // remove end null character
        return valueBuf;
    }
    else
    {
        throw std::runtime_error("Windows system error code: " + std::to_string(rc));
    }
}

int main()
{
    std::wstring regSubKey;
#ifdef _WIN64 // Manually switching between 32bit/64bit for the example. Use dwFlags instead.
    regSubKey = L"SOFTWARE\\WOW6432Node\\Company Name\\Application Name\\";
#else
    regSubKey = L"SOFTWARE\\Company Name\\Application Name\\";
#endif
    std::wstring regValue(L"MyValue");
    std::wstring valueFromRegistry;
    try
    {
        valueFromRegistry = GetStringValueFromHKLM(regSubKey, regValue);
    }
    catch (std::exception& e)
    {
        std::cerr << e.what();
    }
    std::wcout << valueFromRegistry;
}
Run Code Online (Sandbox Code Playgroud)

它的参数dwFlags支持类型限制标志,在失败时用零填充值缓冲区 ( RRF_ZEROONFAILURE) 和 64位程序的32/64 位注册表访问 ( RRF_SUBKEY_WOW6464KEY, RRF_SUBKEY_WOW6432KEY)。


Ser*_*rge 5

RegOpenKeyRegQueryKeyEx这对可以解决问题.

如果使用MFC CRegKey类则更容易解决.