win32应用程序在64-bit系统上运行,读取注册表HKEY_LOCAL_MACHINE\Software信息会被转向,导致失败。需在权限加上标志位KEY_WOW64_64KEY。
同时,Unicode编码条件下,windows系统为宽字符,需要将其转换成窄字符。
void OnReadReg(char *RegValue)
{
HKEY hKey;
TCHAR RegGet[500];
memset(RegGet, '\0', 500);
DWORD dwType = REG_SZ;
DWORD dwLength = 256;
int ret;
ret = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\FFCS_som\\FFCS_som\\settings"), 0, ***KEY_WOW64_64KEY*** | KEY_ALL_ACCESS, &hKey);
if(0 != ret)
{
PutDbgStr(L"Error %d : RegOpenKeyEx not success!", ret);
}
ret = RegQueryValueEx(hKey, _T("ProxyAddr"), 0, &dwType, (LPBYTE)RegGet, &dwLength);
if(0 != ret)
{
PutDbgStr(L"Error %d : RegQueryValueEx not success!", ret);
}
TcharToChar(RegGet, RegValue);
}
void TcharToChar(const TCHAR * tchar, char * _char)
{
int iLength ;
//获取字节长度
iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);
//将tchar值赋给_char
WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);
}
总结: 使用标志位 KEY_WOW64_32KEY 或 KEY_WOW64_64KEY 来控制 HKEY_LOCAL_MACHINE\Software 的访问, 如果你的程序可能会运行在 x64 Windows 上。且二者无法同时存在。详情见MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx
”
Either KEY_WOW64_32KEY or KEY_WOW64_64KEY can be specified. If both flags are specified, the function fails with ERROR_INVALID_PARAMETER.
Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP: If both flags are specified, the function’s behavior is undefined.
“
感谢前辈参考:
这里写链接内容
在64位Windows系统中,32位或64位win32应用程序尝试读取HKEY_LOCAL_MACHINE\Software时,会遇到权限问题。为解决此问题,需使用KEY_WOW64_64KEY标志,确保正确访问注册表。在Unicode环境中,还需处理宽字符到窄字符的转换。根据MSDN,只能选择KEY_WOW64_32KEY或KEY_WOW64_64KEY,不能同时使用,否则会导致错误。
845

被折叠的 条评论
为什么被折叠?



