注册表操作(Win32, C++)

本文介绍了一个用于简化Windows注册表操作的C++类库CRegUtils。该类库提供了读写不同类型的注册表值、创建和删除键等常用功能。

以下为简单的封装操作, 常用的注册表操作做了简化操作, 使用例子写在 main.cpp 中, 具体 注册表api 用法可以参考msdn中的详情.

 

CRegUtils.h

#pragma once

#include <wtypesbase.h>
#include <tchar.h>
#include <string>
#include <vector>

#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif

#define HKCR                (HKEY_CLASSES_ROOT)
#define HKCU                (HKEY_CURRENT_USER)
#define HKLM                (HKEY_LOCAL_MACHINE)
#define HKU                 (HKEY_USERS)

#define HKPD                (HKEY_PERFORMANCE_DATA)
#define HKPT                (HKEY_PERFORMANCE_TEXT)
#define HKPN                (HKEY_PERFORMANCE_NLSTEXT)

#if(WINVER >= 0x0400)
#define HKCC                (HKEY_CURRENT_CONFIG)
#define HKDD                (HKEY_DYN_DATA)
#define HKCULS              (HKEY_CURRENT_USER_LOCAL_SETTINGS)
#endif

class CRegUtils
{
public:

    // 写入 REG_DWORD 类型数据
    static bool SetDword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, DWORD dwValue);

    // 读取 REG_DWORD 类型数据
    static bool GetDword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, DWORD& dwValue);

    // 写入 REG_QWORD 类型数据
    static bool SetQword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, ULONGLONG ullValue);

    // 读取 REG_QWORD 类型数据
    static bool GetQword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, ULONGLONG& ullValue);

    // 写入 REG_SZ 类型数据
    static bool SetString(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, const _tstring& strValue);

    // 读取 REG_SZ 类型数据
    static bool GetString(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, _tstring& strValue);

    // 写入 REG_BINARY 类型数据
    static bool SetBinary(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, LPCVOID lpData, DWORD dwSize);

    // 读取 REG_BINARY 类型数据
    static bool GetBinary(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, LPVOID lpData, DWORD cbSize);

    // 读取 指定值 所需字节大小
    static bool GetValueSize(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, DWORD& cbSize);

    // 创建 指定键
    static bool CreateKey(HKEY hKey, const _tstring& lpSubKey);

    // 删除 指定键(递归删除)
    static bool DelKey(HKEY hKey, const _tstring& lpSubKey);

    // 删除 指定值
    static bool DelValue(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName);

    // 枚举 指定键 的子键
    static bool EnumKeyName(HKEY hKey, const _tstring& lpSubKey, std::vector<_tstring>& keyList);

    // 枚举 指定键 的值名
    static bool EnumValueName(HKEY hKey, const _tstring& lpSubKey, std::vector<_tstring>& valueList);
};

CRegUtils.cpp

#include "CRegUtils.h"

// 注册表大小限制, 参见 https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-element-size-limits
#define REG_KEY_NAME_CCH_LIMITS             (255)            // 注册表键名大小限制(项名称包括注册表中项的绝对路径,始终从基项开始)
#define REG_VALUE_NAME_CB_LIMITS            (MAX_PATH)       // 注册表值名大小限制(260 个 ANSI 字符或 16,383 个 Unicode 字符。)
#define REG_VALUE_DATA_CB_LIMITS            (1024 * 1024)    // 注册表值数据大小限制(可用内存(最新格式) 1 MB(标准格式))

// 写入 REG_DWORD 类型数据
bool CRegUtils::SetDword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, DWORD dwValue)
{
    LSTATUS ls = ERROR_SUCCESS;

    ls = ::RegSetKeyValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), REG_DWORD, &dwValue, sizeof(dwValue));
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    return ERROR_SUCCESS == ls;
}

// 读取 REG_DWORD 类型数据
bool CRegUtils::GetDword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, DWORD& dwValue)
{
    LSTATUS ls = ERROR_SUCCESS;
    DWORD cbSize = sizeof(dwValue);

    ls = ::RegGetValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), RRF_RT_REG_DWORD/* | RRF_ZEROONFAILURE*/, NULL, &dwValue, &cbSize);
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    return ERROR_SUCCESS == ls;
}

// 写入 REG_QWORD 类型数据
bool CRegUtils::SetQword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, ULONGLONG ullValue)
{
    LSTATUS ls = ERROR_SUCCESS;

    ls = ::RegSetKeyValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), REG_QWORD, &ullValue, sizeof(ullValue));
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    return ERROR_SUCCESS == ls;
}

// 读取 REG_QWORD 类型数据
bool CRegUtils::GetQword(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, ULONGLONG& ullValue)
{
    LSTATUS ls = ERROR_SUCCESS;
    DWORD cbSize = sizeof(ullValue);

    ls = ::RegGetValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), RRF_RT_REG_QWORD/* | RRF_ZEROONFAILURE*/, NULL, &ullValue, &cbSize);
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    return ERROR_SUCCESS == ls;
}

// 写入 REG_SZ 类型数据
bool CRegUtils::SetString(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, const _tstring& strValue)
{
    LSTATUS ls = ERROR_SUCCESS;

    ls = ::RegSetKeyValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), REG_SZ, strValue.c_str(), (DWORD)((strValue.length() + 1) * sizeof(TCHAR)));
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    return ERROR_SUCCESS == ls;
}

// 读取 REG_SZ 类型数据
bool CRegUtils::GetString(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, _tstring& strValue)
{
    LSTATUS ls = ERROR_SUCCESS;
    DWORD dwSize = 0;
    LPVOID lpBuf = NULL;

    //获取数据长度
    ls = ::RegGetValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), RRF_RT_REG_SZ/* | RRF_ZEROONFAILURE*/, NULL, 0, &dwSize);
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    // 数据长度超过常规大小限制, 则重新分配空间
    lpBuf = HeapAlloc(GetProcessHeap(), 0, dwSize);
    if (lpBuf)
    {
        ls = ::RegGetValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), RRF_RT_REG_SZ /*| RRF_ZEROONFAILURE*/, NULL, lpBuf, &dwSize);
        if (ERROR_SUCCESS == ls)
        {
            strValue = (LPCTSTR)lpBuf;
        }

        HeapFree(GetProcessHeap(), 0, lpBuf);
    }

    return ERROR_SUCCESS == ls;
}

// 写入 REG_BINARY 类型数据
bool CRegUtils::SetBinary(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, LPCVOID lpData, DWORD cbSize)
{
    LSTATUS ls = ERROR_SUCCESS;
    ls = ::RegSetKeyValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), REG_BINARY, lpData, cbSize);
    return ERROR_SUCCESS == ls;
}

// 读取REG_BINARY类型数据
bool CRegUtils::GetBinary(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, LPVOID lpData, DWORD cbSize)
{
    LSTATUS ls = ERROR_SUCCESS;
    ls = ::RegGetValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), RRF_RT_REG_BINARY/* | RRF_ZEROONFAILURE*/, NULL, lpData, &cbSize);
    return ERROR_SUCCESS == ls;
}

// 读取数据所需字节大小
bool CRegUtils::GetValueSize(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName, DWORD& cbSize)
{
    LSTATUS ls = ERROR_SUCCESS;
    ls = ::RegGetValue(hKey, lpSubKey.c_str(), lpValueName.c_str(), RRF_RT_ANY, NULL, nullptr, &cbSize);
    return (ERROR_SUCCESS == ls) || (ERROR_MORE_DATA == ls);
}

// 创建 指定键
bool CRegUtils::CreateKey(HKEY hKey, const _tstring& lpSubKey)
{
    LSTATUS ls = ERROR_SUCCESS;
    HKEY hNewKey = NULL;
    DWORD dwDisposition = 0;

    ls = ::RegCreateKeyEx(hKey, lpSubKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hNewKey, &dwDisposition);

    if (hNewKey)
    {
        ::RegCloseKey(hNewKey);
    }

    return ERROR_SUCCESS == ls;
}

// 删除 指定键
bool CRegUtils::DelKey(HKEY hKey, const _tstring& lpSubKey)
{
    LSTATUS ls = ERROR_SUCCESS;
    ls = ::RegDeleteTree(hKey, lpSubKey.c_str());
    return ERROR_SUCCESS == ls;
}

// 删除 指定值
bool CRegUtils::DelValue(HKEY hKey, const _tstring& lpSubKey, const _tstring& lpValueName)
{
    LSTATUS ls = ERROR_SUCCESS;
    ls = ::RegDeleteKeyValue(hKey, lpSubKey.c_str(), lpValueName.c_str());
    return ERROR_SUCCESS == ls;
}

// 枚举 指定键 的子键
bool CRegUtils::EnumKeyName(HKEY hKey, const _tstring& lpSubKey, std::vector<_tstring>& keyList)
{
    TCHAR szKeyName[MAX_PATH] = { 0 };
    DWORD dwCchName = _countof(szKeyName);
    LSTATUS ls = ERROR_SUCCESS;
    HKEY hResultKey = NULL;
    DWORD dwIndex = 0;

    keyList.clear();

    ls = ::RegOpenKeyEx(hKey, lpSubKey.c_str(), 0, KEY_READ, &hResultKey);
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    do
    {
        dwCchName = _countof(szKeyName);
        ls = ::RegEnumKeyEx(hResultKey, dwIndex++, szKeyName, &dwCchName, NULL, NULL, NULL, NULL);
        if (ERROR_SUCCESS == ls)
        {
            keyList.push_back(szKeyName);
        }
    } while (ERROR_SUCCESS == ls);

    ::RegCloseKey(hResultKey);

    return true;
}

// 枚举 指定键 的值名
bool CRegUtils::EnumValueName(HKEY hKey, const _tstring& lpSubKey, std::vector<_tstring>& valueList)
{
    TCHAR szValueName[MAX_PATH] = { 0 };
    DWORD dwCchName = _countof(szValueName);
    LSTATUS ls = ERROR_SUCCESS;
    HKEY hResultKey = NULL;
    DWORD dwIndex = 0;

    valueList.clear();

    ls = ::RegOpenKeyEx(hKey, lpSubKey.c_str(), 0, KEY_READ, &hResultKey);
    if (ERROR_SUCCESS != ls)
    {
        return false;
    }

    do
    {
        dwCchName = _countof(szValueName);
        ls = ::RegEnumValue(hResultKey, dwIndex++, szValueName, &dwCchName, NULL, NULL, NULL, NULL);
        if (ERROR_SUCCESS == ls)
        {
            valueList.push_back(szValueName);
        }
    } while (ERROR_SUCCESS == ls);

    ::RegCloseKey(hResultKey);

    return true;
}

main.cpp

// CRegUtils.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "CRegUtils.h"
#include <iostream>

int main()
{
    BYTE data[MAX_PATH] = { 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
    DWORD dwSize = sizeof(data);
    ULONGLONG qwQword = 0;
    DWORD dwDword = 0;
    _tstring strInfo;

    CRegUtils::SetDword(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("DWORD"), 1234);
    CRegUtils::GetDword(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("DWORD"), dwDword);
    CRegUtils::GetValueSize(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("DWORD"), dwSize);


    CRegUtils::SetQword(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("QWORD"), 5678);
    CRegUtils::GetQword(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("QWORD"), qwQword);
    CRegUtils::GetValueSize(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("QWORD"), dwSize);

    CRegUtils::SetString(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("SZ"), _T("1234567890"));
    CRegUtils::GetString(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("SZ"), strInfo);
    CRegUtils::GetValueSize(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("SZ"), dwSize);

    CRegUtils::SetBinary(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("BINARY"), "1234567890", 10);
    CRegUtils::GetBinary(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("BINARY"), data, sizeof(data));
    CRegUtils::GetValueSize(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("BINARY"), dwSize);

    std::vector<_tstring> keyList;
    std::vector<_tstring> valueList;
    CRegUtils::EnumKeyName(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), keyList);
    CRegUtils::EnumValueName(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), valueList);
    CRegUtils::SetString(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T(""), _T("1234567890-="));
    CRegUtils::GetString(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T(""), strInfo);

    CRegUtils::DelValue(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"), _T("BINARY"));
    CRegUtils::DelKey(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"));

    CRegUtils::CreateKey(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000)"));
    CRegUtils::CreateKey(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000\1)"));
    CRegUtils::CreateKey(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000\2)"));
    CRegUtils::CreateKey(HKLM, _T(R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\0000\3)"));

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值