#include "RegUtil.h"
BOOL IsWow64()
{
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL bIsWow64 = FALSE;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(_T("kernel32")), "IsWow64Process");
if (NULL != fnIsWow64Process)
{
fnIsWow64Process(GetCurrentProcess(), &bIsWow64);
}
return bIsWow64;
}
BOOL GetIEPath(_TCHAR IEPath[MAX_PATH])
{
TCHAR SysPath[MAX_PATH] = {0};
GetSystemDirectory(SysPath, MAX_PATH);
memset(IEPath, 0, MAX_PATH * sizeof(_TCHAR));
IEPath[0] = SysPath[0];
if (IsWow64())
{
lstrcat(IEPath, _T(":\\Program Files (x86)\\Internet Explorer\\iexplore.exe"));
}
else
{
lstrcat(IEPath, _T(":\\Program Files\\Internet Explorer\\iexplore.exe"));
}
if (!PathFileExists(IEPath))
{
GetRegValue(IEPath, HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE"), NULL, REG_NONE);
// 严防出现腾讯TT事件
if (!_tcsstr(IEPath, _T("iexplore.exe")))
{
memset(IEPath, 0, MAX_PATH * sizeof(_TCHAR));
}
}
return PathFileExists(IEPath);
}
附: RegUtil
#pragma once
#include <tchar.h>
#include <windows.h>
#ifdef __REG__API
#define REG_API
#else
#define REG_API extern
#endif
namespace RegUtil
{
REG_API LSTATUS SetRegValue(HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwValue);
REG_API LSTATUS SetRegValue(HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR lpszValue);
REG_API LSTATUS SetRegValue(HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, const BYTE* lpData, DWORD cbData, DWORD dwType = REG_SZ);
REG_API LSTATUS GetRegValue(TCHAR* RegValue, HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPDWORD cbData, DWORD dwType);
REG_API LSTATUS GetRegValue(TCHAR* RegValue, HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwType = REG_SZ);
}
#include "stdafx.h"
#ifndef __REG__API
#define __REG__API
#endif
#include "RegUtil.h"
#include <Aclapi.h>
#include <shobjidl.h>
namespace RegUtil
{
LSTATUS SetRegValue(HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, const BYTE* lpData, DWORD cbData, DWORD dwType)
{
LSTATUS RetVal;
HKEY hKey;
const REGSAM samDesired = PCInfo::IsWow64() ? KEY_SET_VALUE | KEY_WOW64_64KEY : KEY_SET_VALUE;
if (ERROR_SUCCESS != RegOpenKeyEx(MainKey, lpSubKey, 0, samDesired, &hKey))
{
HKEY phkResult;
RetVal = RegCreateKey(MainKey, lpSubKey, &phkResult);
if (RetVal == ERROR_SUCCESS)
{
RetVal = RegOpenKeyEx(MainKey, lpSubKey, 0, samDesired, &hKey);
if (RetVal != ERROR_SUCCESS)
return RetVal;
}
else
{
return RetVal;
}
}
RetVal = RegSetValueEx(hKey, lpValueName, NULL, dwType, lpData, cbData);
RegCloseKey(hKey);
return RetVal;
}
LSTATUS SetRegValue(HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwValue)
{
return SetRegValue(MainKey, lpSubKey, lpValueName, (BYTE *)&dwValue, sizeof(DWORD), REG_DWORD);
}
LSTATUS SetRegValue(HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR lpszValue)
{
#ifdef UNICODE
DWORD cbData = 2 * lstrlenW(lpszValue);
#else
DWORD cbData = lstrlenA(lpszValue);
#endif
return SetRegValue(MainKey, lpSubKey, lpValueName, (BYTE *)lpszValue, cbData);
}
LSTATUS GetRegValue(TCHAR* RegValue, HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPDWORD cbData, DWORD dwType)
{
LSTATUS RetVal = ERROR_SUCCESS;
HKEY hKey;
const REGSAM samDesired = PCInfo::IsWow64() ? KEY_READ | KEY_WOW64_64KEY : KEY_READ;
RetVal = RegOpenKeyEx(MainKey, lpSubKey, 0, samDesired, &hKey);
if (RetVal == ERROR_SUCCESS)
{
RetVal = RegQueryValueEx(hKey, lpValueName, NULL, &dwType , (LPBYTE)RegValue, cbData);
RegCloseKey(hKey);
}
return RetVal;
}
LSTATUS GetRegValue(TCHAR* RegValue, HKEY MainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwType)
{
DWORD cbData = 512;
return GetRegValue(RegValue, MainKey, lpSubKey, lpValueName, &cbData, dwType);
}
}