关于C ++:如何从Windows注册表读取值

关于C ++:如何从Windows注册表读取值

How to read a value from the Windows registry

给定某个注册表值的键(例如HKEY_LOCAL_MACHINE \ blah \ blah \ blah \ foo),我该如何:

  • 安全地确定存在这样的密钥。
  • 以编程方式(即使用代码)获得其价值。
  • 我绝对不打算将任何内容写回注册表(如果可以的话,在我的职业生涯中)。 因此,如果我不正确地写注册表,我们可以跳过关于体内每个分子以光速爆炸的讲座。

    首选C ++的答案,但大多数情况下,您只需要知道要获得该值的特殊Windows API含义是什么。


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

  • 如果注册表项存在
  • 该注册表项的默认值是多少
  • 什么是字符串值
  • 什么是DWORD值
  • 示例代码:

    包括库依赖项:Advapi32.lib

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    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;
    }


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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

    由于Windows> = Vista / Server 2008,因此可以使用RegGetValue,它比RegQueryValueEx安全。无需对字符串值(REG_SZREG_MULTI_SZREG_EXPAND_SZ)进行RegOpenKeyExRegCloseKeyNUL终止检查。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    #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;
    }

    它的参数dwFlags支持类型限制标志,在发生故障(RRF_ZEROONFAILURE)和64位程序的32/64位注册表访问(RRF_SUBKEY_WOW6464KEYRRF_SUBKEY_WOW6432KEY)时,将值缓冲区填充为零。


    RegOpenKey和RegQueryKeyEx对将解决问题。

    如果使用MFC,则CRegKey类是更简单的解决方案。


    RegQueryValueEx

    如果值存在,则给出值,如果键不存在,则返回错误代码ERROR_FILE_NOT_FOUND。

    (我无法确定我的链接是否有效,但是如果您只是用谷歌搜索" RegQueryValueEx",则第一个匹配项就是msdn文档。)


    通常,寄存器键和值是程序中的常数。如果是这样,下面是一个示例如何读取DWORD注册表值Computer\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\FileSystem\\LongPathsEnabled

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <windows.h>

    DWORD val;
    DWORD dataSize = sizeof(val);
    if (ERROR_SUCCESS == RegGetValueA(HKEY_LOCAL_MACHINE,"SYSTEM\\\\CurrentControlSet\\\\Control\\\\FileSystem","LongPathsEnabled", RRF_RT_DWORD, nullptr /*type not required*/, &val, &dataSize)) {
      printf("Value is %i\
    "
    , val);
      // no CloseKey needed because it is a predefined registry key
    }
    else {
      printf("Error reading.\
    "
    );
    }

    要适应其他值类型,请参阅https://docs.microsoft.com/zh-cn/windows/win32/api/winreg/nf-winreg-reggetvaluea了解完整规范。


    该控制台应用程序将从大多数可能的注册表值的注册表项中列出所有值及其数据。有些奇怪的东西不经常使用。如果需要全部支持,请参考此注册表值类型文档,从此示例展开。

    将此作为您可以从.reg文件格式导入的注册表项内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\\added\\subkey]
    "String_Value"="hello, world!"
    "Binary_Value"=hex:01,01,01,01
    "Dword value"=dword:00001224
    "QWord val"=hex(b):24,22,12,00,00,00,00,00
    "multi-line val"=hex(7):4c,00,69,00,6e,00,65,00,20,00,30,00,00,00,4c,00,69,00,\\
      6e,00,65,00,20,00,31,00,00,00,4c,00,69,00,6e,00,65,00,20,00,32,00,00,00,00,\\
      00
    "expanded_val"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,\\
      00,4c,00,45,00,25,00,5c,00,6e,00,65,00,77,00,5f,00,73,00,74,00,75,00,66,00,\\
      66,00,00,00

    控制台应用程序本身:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    #include <Windows.h>
    #include <iostream>
    #include <string>
    #include <locale>
    #include <vector>
    #include <iomanip>

    int wmain()
    {
        const auto hKey = HKEY_CURRENT_USER;
        constexpr auto lpSubKey = TEXT("added\\\\subkey");
        auto openedKey = HKEY();
        auto status = RegOpenKeyEx(hKey, lpSubKey, 0, KEY_READ, &openedKey);

        if (status == ERROR_SUCCESS) {
            auto valueCount = static_cast<DWORD>(0);
            auto maxNameLength = static_cast<DWORD>(0);
            auto maxValueLength = static_cast<DWORD>(0);
            status = RegQueryInfoKey(openedKey, NULL, NULL, NULL, NULL, NULL, NULL,
                &valueCount, &maxNameLength, &maxValueLength, NULL, NULL);

            if (status == ERROR_SUCCESS) {
                DWORD type = 0;
                DWORD index = 0;
                std::vector<wchar_t> valueName = std::vector<wchar_t>(maxNameLength + 1);
                std::vector<BYTE> dataBuffer = std::vector<BYTE>(maxValueLength);

                for (DWORD index = 0; index < valueCount; index++) {
                    DWORD charCountValueName = static_cast<DWORD>(valueName.size());
                    DWORD charBytesData = static_cast<DWORD>(dataBuffer.size());
                    status = RegEnumValue(openedKey, index, valueName.data(), &charCountValueName,
                        NULL, &type, dataBuffer.data(), &charBytesData);

                    if (type == REG_SZ) {
                        const auto reg_string = reinterpret_cast<wchar_t*>(dataBuffer.data());
                        std::wcout << L"Type: REG_SZ" << std::endl;
                        std::wcout << L"\\tName:" << valueName.data() << std::endl;
                        std::wcout << L"\\tData :" << reg_string << std::endl;
                    }
                    else if (type == REG_EXPAND_SZ) {
                        const auto casted = reinterpret_cast<wchar_t*>(dataBuffer.data());
                        TCHAR buffer[32000];
                        ExpandEnvironmentStrings(casted, buffer, 32000);
                        std::wcout << L"Type: REG_EXPAND_SZ" << std::endl;
                        std::wcout << L"\\tName:" << valueName.data() << std::endl;
                        std::wcout << L"\\tData:" << buffer << std::endl;
                    }
                    else if (type == REG_MULTI_SZ) {
                        std::vector<std::wstring> lines;
                        const auto str = reinterpret_cast<wchar_t*>(dataBuffer.data());
                        auto line = str;
                        lines.emplace_back(line);
                        for (auto i = 0; i < charBytesData / sizeof(wchar_t) - 1; i++) {
                            const auto c = str[i];
                            if (c == 0) {
                                line = str + i + 1;
                                const auto new_line = reinterpret_cast<wchar_t*>(line);
                                if (wcsnlen_s(new_line, 1024) > 0)
                                    lines.emplace_back(new_line);
                            }
                        }
                        std::wcout << L"Type: REG_MULTI_SZ" << std::endl;
                        std::wcout << L"\\tName:" << valueName.data() << std::endl;
                        std::wcout << L"\\tData:" << std::endl;
                        for (size_t i = 0; i < lines.size(); i++) {
                            std::wcout << L"\\t\\tLine[" << i + 1 << L"]:" << lines[i] << std::endl;
                        }
                    }
                    if (type == REG_DWORD) {
                        const auto dword_value = reinterpret_cast<unsigned long*>(dataBuffer.data());
                        std::wcout << L"Type: REG_DWORD" << std::endl;
                        std::wcout << L"\\tName:" << valueName.data() << std::endl;
                        std::wcout << L"\\tData :" << std::to_wstring(*dword_value) << std::endl;
                    }
                    else if (type == REG_QWORD) {
                        const auto qword_value = reinterpret_cast<unsigned long long*>(dataBuffer.data());
                        std::wcout << L"Type: REG_DWORD" << std::endl;
                        std::wcout << L"\\tName:" << valueName.data() << std::endl;
                        std::wcout << L"\\tData :" << std::to_wstring(*qword_value) << std::endl;
                    }
                    else if (type == REG_BINARY) {
                        std::vector<uint16_t> bins;
                        for (auto i = 0; i < charBytesData; i++) {
                            bins.push_back(static_cast<uint16_t>(dataBuffer[i]));
                        }
                        std::wcout << L"Type: REG_BINARY" << std::endl;
                        std::wcout << L"\\tName:" << valueName.data() << std::endl;
                        std::wcout << L"\\tData:";
                        for (size_t i = 0; i < bins.size(); i++) {
                            std::wcout << L"" << std::uppercase << std::hex << \\
                                std::setw(2) << std::setfill(L'0') << std::to_wstring(bins[i]);
                        }
                        std::wcout << std::endl;
                    }
                }
            }
        }

        RegCloseKey(openedKey);
        return 0;
    }

    预期的控制台输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    Type: REG_SZ
            Name: String_Value
            Data : hello, world!
    Type: REG_BINARY
            Name: Binary_Value
            Data: 01 01 01 01
    Type: REG_DWORD
            Name: Dword value
            Data : 4644
    Type: REG_DWORD
            Name: QWord val
            Data : 1188388
    Type: REG_MULTI_SZ
            Name: multi-line val
            Data:
                    Line[1]: Line 0
                    Line[2]: Line 1
                    Line[3]: Line 2
    Type: REG_EXPAND_SZ
            Name: expanded_val
            Data: C:\\Users\\user name\
    ew_stuff

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    #include <windows.h>
    #include <map>
    #include <string>
    #include <stdio.h>
    #include <string.h>
    #include <tr1/stdint.h>

    using namespace std;

    void printerr(DWORD dwerror) {
        LPVOID lpMsgBuf;
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            dwerror,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL
        );
        // Process any inserts in lpMsgBuf.
        // ...
        // Display the string.
        if (isOut) {
            fprintf(fout,"%s\
    "
    , lpMsgBuf);
        } else {
            printf("%s\
    "
    , lpMsgBuf);
        }
        // Free the buffer.
        LocalFree(lpMsgBuf);
    }



    bool regreadSZ(string& hkey, string& subkey, string& value, string& returnvalue, string& regValueType) {
        char s[128000];
        map<string,HKEY> keys;
        keys["HKEY_CLASSES_ROOT"]=HKEY_CLASSES_ROOT;
        keys["HKEY_CURRENT_CONFIG"]=HKEY_CURRENT_CONFIG; //DID NOT SURVIVE?
        keys["HKEY_CURRENT_USER"]=HKEY_CURRENT_USER;
        keys["HKEY_LOCAL_MACHINE"]=HKEY_LOCAL_MACHINE;
        keys["HKEY_USERS"]=HKEY_USERS;
        HKEY mykey;

        map<string,DWORD> valuetypes;
        valuetypes["REG_SZ"]=REG_SZ;
        valuetypes["REG_EXPAND_SZ"]=REG_EXPAND_SZ;
        valuetypes["REG_MULTI_SZ"]=REG_MULTI_SZ; //probably can't use this.

        LONG retval=RegOpenKeyEx(
            keys[hkey],         // handle to open key
            subkey.c_str(),  // subkey name
            0,   // reserved
            KEY_READ, // security access mask
            &mykey    // handle to open key
        );
        if (ERROR_SUCCESS != retval) {printerr(retval); return false;}
        DWORD slen=128000;
        DWORD valuetype = valuetypes[regValueType];
        retval=RegQueryValueEx(
          mykey,            // handle to key
          value.c_str(),  // value name
          NULL,   // reserved
          (LPDWORD) &valuetype,       // type buffer
          (LPBYTE)s,        // data buffer
          (LPDWORD) &slen      // size of data buffer
        );
        switch(retval) {
            case ERROR_SUCCESS:
                //if (isOut) {
                //    fprintf(fout,"RegQueryValueEx():ERROR_SUCCESS:succeeded.\
    ");

                //} else {
                //    printf("RegQueryValueEx():ERROR_SUCCESS:succeeded.\
    ");

                //}
                break;
            case ERROR_MORE_DATA:
                //what do I do now?  data buffer is too small.
                if (isOut) {
                    fprintf(fout,"RegQueryValueEx():ERROR_MORE_DATA: need bigger buffer.\
    "
    );
                } else {
                    printf("RegQueryValueEx():ERROR_MORE_DATA: need bigger buffer.\
    "
    );
                }
                return false;
            case ERROR_FILE_NOT_FOUND:
                if (isOut) {
                    fprintf(fout,"RegQueryValueEx():ERROR_FILE_NOT_FOUND: registry value does not exist.\
    "
    );
                } else {
                    printf("RegQueryValueEx():ERROR_FILE_NOT_FOUND: registry value does not exist.\
    "
    );
                }
                return false;
            default:
                if (isOut) {
                    fprintf(fout,"RegQueryValueEx():unknown error type 0x%lx.\
    "
    , retval);
                } else {
                    printf("RegQueryValueEx():unknown error type 0x%lx.\
    "
    , retval);
                }
                return false;

        }
        retval=RegCloseKey(mykey);
        if (ERROR_SUCCESS != retval) {printerr(retval); return false;}

        returnvalue = s;
        return true;
    }


    推荐阅读