#include <windows.h>

typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
BOOL RegWriteString(HKEY hKey, string keyName, string keyValue, bool valueResult)
{
if (keyValue == "") {
return valueResult;
}
return RegSetValueEx(hKey, keyName.c_str(), 0, REG_SZ, (const BYTE*)keyValue.c_str(), strlen(keyValue.c_str()));
}

LPFN_ISWOW64PROCESS
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetModuleHandle("kernel32"), "IsWow64Process");

BOOL SystemUtil::IsWow64()
{
BOOL bIsWow64 = FALSE;

if (NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
{
// handle error
}
}
return bIsWow64;
}
/// <summary>
/// 读取指定的key
/// </summary>
/// <param name="displayName"></param>
/// <returns></returns>
/// <summary>
/// 获取32/64对应的注册表值
/// </summary>
/// <returns></returns>
string GetUninstallKey() {
if (IsWow64()) {
return "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
}
else {
return "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
}
}
CString GetRegKeyString(string displayName, const char* subKey) {
string key = GetUninstallKey() + displayName;
LONG ires;
HKEY hMyKey;
DWORD Type = REG_SZ;
DWORD count = 256;
byte mstr[256] = "";
ires = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0,
KEY_READ, &hMyKey);
if (ERROR_SUCCESS != ires)
return "";
else
ires = RegQueryValueEx(hMyKey, subKey, 0, &Type,
mstr, &count);
ires = RegCloseKey(hMyKey);
CString tms = (LPCTSTR)mstr;

return tms;
}

读取注册表DWORD

  HKEY key;
LPBYTE path_Get = new BYTE[254];
DWORD type = REG_SZ;
DWORD dwBytes = 254;

DWORD dwordValue = 0;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "key", 0, KEY_READ, &key) == ERROR_SUCCESS)
{
if (RegQueryValueEx(key, "item", NULL, &type, (PBYTE)&dwordValue, &dwBytes) == ERROR_SUCCESS) {
//dwordValue 为实际值
}
}

留待后查,同时方便他人