// 头文件 RegClass.h
#pragma once
class RegOperateClass
{
public:
// 读取注册表REG_SZ类型的数据
static BOOL RegGetStringValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, CAtlStringW& strValue);
static BOOL RegSetStringValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, LPCWSTR lpszValue);
// 读取注册表REG_DWORD类型的数据
static BOOL RegGetDWORDValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, DWORD& dwValue);
static BOOL RegSetDWORDValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, DWORD dwValue);
static BOOL RegKeyCreate(HKEY hKey, LPCWSTR lpszSubKey);
static BOOL RegKeyRemove(HKEY hKey, LPCWSTR lpszSubKey);
static BOOL RegValueRemove(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName);
};
// cpp文件 RegClass.cpp
#include <Windows.h>
#include <atlstr.h>
using namespace std;
#include "RegClass.h"
// 函数声明
static
BOOL
_IsWow64App();
// 判断是否是64位OS上32位进程访问64位注册表视图
// 如果是的话则加上KEY_WOW64_64KEY标志
REGSAM g_64RegView = _IsWow64App() ? KEY_WOW64_64KEY : 0x0;
/**********************************************************
* 辅助函数
***********************************************************/
// 检查当前进程是否为 32 位进程运行在 64 位操作系统上
BOOL
_IsWow64App()
{
BOOL isWow64 = FALSE;
if (IsWow64Process(GetCurrentProcess(), &isWow64)){
return isWow64 != 0;
}
// 如果无法确定是否为 32 位进程,则返回 false
return isWow64;
}
// 创建Or打开注册表项
static
HKEY
_CreateOrOpenKey(HKEY hKey, LPCWSTR lpszSubKey, BOOL bUnexistOnlyCreate = FALSE, DWORD dwPermissions = KEY_ALL_ACCESS)
{
HKEY hSubKey = NULL;
if (bUnexistOnlyCreate) {
RegCreateKeyEx(hKey, lpszSubKey, 0, 0, REG_OPTION_NON_VOLATILE/* 默认*/, dwPermissions, NULL, &hSubKey, NULL);
}
else {
RegOpenKeyEx(hKey, lpszSubKey, 0x0, dwPermissions, &hSubKey);
}
return hSubKey;
}
/**********************************************************
* 静态函数
***********************************************************/
BOOL RegOperateClass::RegGetStringValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, CAtlStringW& strValue)
{
DWORD cbData = 0x0;
DWORD dwRet = 0x0;
DWORD dwRegType = REG_SZ;
LPBYTE PerfData = NULL;
HKEY hSubKey = _CreateOrOpenKey(hKey, lpszSubKey, TRUE, KEY_ALL_ACCESS | g_64RegView);
if (!hSubKey){
return FALSE;
}
if (ERROR_SUCCESS == RegQueryValueExW(hSubKey, lpszValueName, 0, &dwRegType, NULL, &cbData))
{
PerfData = (LPBYTE)LocalAlloc(LPTR, cbData);
dwRet = RegQueryValueExW(hSubKey, lpszValueName, 0, &dwRegType, PerfData, &cbData);
if (ERROR_SUCCESS == dwRet){
strValue.Format(L"%ws", LPWSTR(PerfData));
}
if (PerfData){
LocalFree(PerfData);
}
}
return dwRet;
}
BOOL RegOperateClass::RegSetStringValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, LPCWSTR lpszValue)
{
return ERROR_SUCCESS == RegSetValueExW(_CreateOrOpenKey(hKey, lpszSubKey, TRUE, KEY_ALL_ACCESS | g_64RegView), lpszValueName, 0, REG_SZ, (LPCBYTE)lpszValue, (DWORD)wcslen(lpszValue) * sizeof(WCHAR));
}
BOOL RegOperateClass::RegGetDWORDValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, DWORD& dwValue)
{
DWORD dwRegType = REG_DWORD;
DWORD dwLength = sizeof(DWORD);
return ERROR_SUCCESS == RegQueryValueExW(_CreateOrOpenKey(hKey, lpszSubKey, FALSE, KEY_READ | g_64RegView), lpszValueName, 0, &dwRegType, (LPBYTE)&dwValue, &dwLength);
}
BOOL RegOperateClass::RegSetDWORDValue(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName, DWORD dwValue)
{
return ERROR_SUCCESS == RegSetValueExW(_CreateOrOpenKey(hKey, lpszSubKey, TRUE, KEY_ALL_ACCESS | g_64RegView), lpszValueName, 0, REG_DWORD, (LPCBYTE)&dwValue, sizeof(DWORD));
}
BOOL RegOperateClass::RegKeyCreate(HKEY hKey, LPCWSTR lpszSubKey)
{
return NULL != _CreateOrOpenKey(hKey, lpszSubKey, TRUE, KEY_ALL_ACCESS | g_64RegView);
}
BOOL RegOperateClass::RegKeyRemove(HKEY hKey, LPCWSTR lpszSubKey)
{
// 先删除SubKey的子项及其值, 再删除项本身
return (ERROR_SUCCESS == RegDeleteTreeW(_CreateOrOpenKey(hKey, lpszSubKey, FALSE, KEY_ALL_ACCESS | g_64RegView), NULL)) ? (ERROR_SUCCESS == RegDeleteKeyW(hKey, lpszSubKey)) : FALSE;
}
BOOL RegOperateClass::RegValueRemove(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValueName)
{
return ERROR_SUCCESS == RegDeleteValueW(_CreateOrOpenKey(hKey, lpszSubKey, FALSE, KEY_ALL_ACCESS | g_64RegView), lpszValueName);
}