win7 64下RegOpenKeyEx返回的值不正确(转)

环境:win7 64位,vs2010,32位dll应用程序

	HKEY hkey;
	// if exist "HKEY_LOCAL_MACHINE\SOFTWARE\Apple Computer, Inc.\QuickTime",return
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Apple Computer, Inc.\\QuickTime"), 0, samDesiredOpen, &hkey) == ERROR_SUCCESS)
	{
		RegCloseKey(hkey);
		return 0;
	}

	// create key
	if(RegCreateKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Apple Computer, Inc.\\QuickTime"), &hkey) != ERROR_SUCCESS)
		return 0;
本来是想手动新建注册表项,结果注册表明明没东西,却返回ERROR_SUCCESS,百度一下

解决:

使用RegEnableReflectionKey/RegDisableReflectionKey,并且加上KEY_WOW64_64KEY

需要先判断是不是64位操作系统

void SafeGetNativeSystemInfo(__out LPSYSTEM_INFO lpSystemInfo)
{
    if (NULL==lpSystemInfo)    return;
    typedef VOID (WINAPI *LPFN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);
    LPFN_GetNativeSystemInfo fnGetNativeSystemInfo = (LPFN_GetNativeSystemInfo)GetProcAddress( GetModuleHandle(_T("kernel32")), "GetNativeSystemInfo");;
    if (NULL != fnGetNativeSystemInfo)
    {
        fnGetNativeSystemInfo(lpSystemInfo);
    }
    else
    {
        GetSystemInfo(lpSystemInfo);
    }
}
再操作注册表

REGSAM samDesiredOpen = KEY_ALL_ACCESS;
SYSTEM_INFO si;
SafeGetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )
{
    samDesiredOpen |= KEY_WOW64_64KEY;
}
RegEnableReflectionKey(HKEY_LOCAL_MACHINE);
HKEY hkey;
// if exist "HKEY_LOCAL_MACHINE\SOFTWARE\Apple Computer, Inc.\QuickTime",return
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Apple Computer, Inc.\\QuickTime"), 0, samDesiredOpen, &hkey) == ERROR_SUCCESS)
{
    RegCloseKey(hkey);
    return 0;
}
RegDisableReflectionKey(HKEY_LOCAL_MACHINE);


参考:http://bbs.csdn.net/topics/380026254

http://bbs.pediy.com/showthread.php?t=159321

http://wenku.baidu.com/link?url=NtpRZQ-6SFkgjCSJTdmnEVlRO233YiKx0Ukvh0HZcu3zSDsUQQXCK2g-KoBeld-s-t46VNsrRPh3O0bAqmwkWuv0Pc_Y4Nvm2B_pk-gZZ3K

https://msdn.microsoft.com/en-us/library/ms724897%28v=VS.85%29.aspx

https://msdn.microsoft.com/en-us/library/ms724072%28v=vs.85%29.aspx

你可能感兴趣的:(win7,64位,RegOpenKeyEx)