WOW64 上的注册表重定向示例
以下示例代码演示 64 位 Windows 上的注册表重定向程序提供的注册表的单独视图。 它还演示了如何根据密钥是共享还是重定向密钥来设置键值。 有关详细信息,请参阅 受 WOW64 影响的注册表项。
为 32 位和 64 位 Windows 分别编译以下代码。 在 64 位 Windows 上运行每个生成的可执行文件,并比较输出。 源代码下面列出了这两个版本的示例输出。
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "advapi32.lib")
// Define application constants.
#if defined(_WIN64)
#define VIEW_DATA L"Hello! 64-bit World"
#define ALT_VIEW_DATA L"Hello! 32-bit World"
#define CROSS_ACCESS KEY_WOW64_32KEY
#else
#define VIEW_DATA L"Hello! 32-bit World"
#define ALT_VIEW_DATA L"Hello! 64-bit World"
#define CROSS_ACCESS KEY_WOW64_64KEY
#endif
// Create a registry key and set its value.
// Return TRUE if the function succeeds, FALSE if it fails.
BOOL
CreateRegistryKeyValue(
HKEY hKeyParent,
PWCHAR KeyName,
REGSAM rsAccessMask,
REGSAM rsSamViewFlag,
PBYTE pValue,
DWORD dwValueSize
)
{
DWORD dwDisposition;
HKEY hKey;
DWORD dwRet;
dwRet =
RegCreateKeyEx(
hKeyParent,
KeyName,
0,
NULL,
REG_OPTION_NON_VOLATILE,
rsAccessMask | rsSamViewFlag,
NULL,
&hKey,
&dwDisposition);
if (dwRet != ERROR_SUCCESS)
{
printf("Error opening or creating key.\n");
return FALSE;
}
// Attempt to set the value of the key.
// If the call fails, close the key and return.
if (ERROR_SUCCESS !=
RegSetValueEx(
hKey,
NULL,
0,
REG_SZ,
pValue,
dwValueSize))
{
printf("Could not set registry value.\n");
RegCloseKey(hKey);
return FALSE;
}
RegCloseKey(hKey);
return TRUE;
}
// Access a registry key and print its value.
// Return TRUE if the function succeeds, FALSE if it fails.
BOOL
AccessRegistryKeyValue(
HKEY hKeyParent,
PWCHAR KeyName,
REGSAM rsAccessMask,
REGSAM rsSamViewFlag
)
{
HKEY hKey;
WCHAR Buffer[MAX_PATH];
DWORD dwSize = sizeof(Buffer);
DWORD dwType;
DWORD dwRet;
dwRet =
RegOpenKeyEx(
hKeyParent,
KeyName,
0,
rsAccessMask | rsSamViewFlag,
&hKey);
if (dwRet != ERROR_SUCCESS)
{
printf("Error opening key!\n");
return FALSE;
}
if (ERROR_SUCCESS !=
RegQueryValueEx(
hKey,
NULL,
0,
&dwType,
(PBYTE)Buffer,
&dwSize))
{
printf("Could not read registry value.\n");
RegCloseKey(hKey);
return FALSE;
}
if (rsSamViewFlag != 0)
{
printf("Alternate view: %S\n", Buffer);
}
else
{
printf("Default view: %S\n", Buffer);
}
RegCloseKey(hKey);
return TRUE;
}
int
main()
{
BOOL Res;
// Define the list of keys to work with.
typedef struct {
HKEY hkRoot;
LPWSTR szRootName;
LPWSTR szName;
}KEYDATA;
KEYDATA Keys[] =
{
{ HKEY_LOCAL_MACHINE, L"HKLM", L"Software\\Hello World" },
{ HKEY_CLASSES_ROOT, L"HKCR", L"Hello" },
{ HKEY_CLASSES_ROOT, L"HKCR", L"CLSID\\{00000000-0000-0000-0000-ABCD00000000}" },
{ HKEY_CLASSES_ROOT, L"HKCR", L"CLSID\\{00000000-0000-0000-0000-ABCD00000000}\\InprocServer32" },
{ HKEY_CLASSES_ROOT, L"HKCR", L"CLSID\\{00000000-0000-0000-0000-ABCD00000000}\\LocalServer32" }
};
// Now create the keys.
for (int i = 0; i < _countof(Keys); i++)
{
// Create the keys in the alternate view of the registry.
Res =
CreateRegistryKeyValue(
Keys[i].hkRoot,
Keys[i].szName,
KEY_READ | KEY_WRITE,
CROSS_ACCESS,
(PBYTE)ALT_VIEW_DATA,
sizeof(ALT_VIEW_DATA));
if (Res == FALSE)
{
printf("Could not create keys in alternate view of the registry.\n");
return 1;
}
// Create the keys in the default view of the registry.
Res =
CreateRegistryKeyValue(
Keys[i].hkRoot,
Keys[i].szName,
KEY_READ | KEY_WRITE,
0,
(PBYTE)VIEW_DATA,
sizeof(VIEW_DATA));
if (Res == FALSE)
{
printf("Could not create keys in default view of the registry.\n");
return 1;
}
}
// Access the registry and print key values.
printf("Application string: %S\n", VIEW_DATA);
for (int i = 0; i < _countof(Keys); i++)
{
printf("%S\\%S\n", Keys[i].szRootName, Keys[i].szName);
Res =
AccessRegistryKeyValue(
Keys[i].hkRoot,
Keys[i].szName,
KEY_READ,
0);
if (Res == FALSE)
{
printf("Unable to access default view of registry.\n");
return 1;
}
// Calls with CROSS_ACCESS explicitly access the alternate registry view.
Res =
AccessRegistryKeyValue(
Keys[i].hkRoot,
Keys[i].szName,
KEY_READ,
CROSS_ACCESS);
if (Res == FALSE)
{
printf("Unable to access alternate view of registry.\n");
return 1;
}
}
return 0;
}
运行该示例的 64 位版本时,会生成以下输出。 HKCR\Hello 的默认视图和备用视图的值相同,因为此密钥是共享的。 其他键的值不同,因为它们是重定向的。
Windows Server 2008、Windows Vista、Windows Server 2003 和 Windows XP: 当这些操作系统上运行此示例时,LocalServer32 键的默认视图和备用视图具有相同的值。 这是因为 LocalServer32 项会重定向并 反映,这会导致其值在关闭密钥的句柄后立即在注册表的 64 位和 32 位视图之间进行同步。 从 Windows 7 开始删除注册表反射。 有关详细信息,请参阅 注册表反射。
Application string: Hello! 64-bit World
HKLM\Software\Hello World
Default view: Hello! 64-bit World
Alternate view: Hello! 32-bit World
HKCR\Hello
Default view: Hello! 64-bit World
Alternate view: Hello! 64-bit World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}
Default view: Hello! 64-bit World
Alternate view: Hello! 32-bit World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}\InprocServer32
Default view: Hello! 64-bit World
Alternate view: Hello! 32-bit World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}\LocalServer32
Default view: Hello! 64-bit World
Alternate view: Hello! 32-bit World
运行该示例的 32 位版本时,它将生成以下输出。 对于 32 位应用程序,注册表的 64 位视图是备用视图,因此值将反转,HKCR\Hello 除外,这是一个共享键。
Application string: Hello! 32-bit World
HKLM\Software\Hello World
Default view: Hello! 32-bit World
Alternate view: Hello! 64-bit World
HKCR\Hello
Default view: Hello! 32-bit World
Alternate view: Hello! 32-bit World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}
Default view: Hello! 32-bit World
Alternate view: Hello! 64-bit World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}\InprocServer32
Default view: Hello! 32-bit World
Alternate view: Hello! 64-bit World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}\LocalServer32
Default view: Hello! 32-bit World
Alternate view: Hello! 64-bit World
若要检查使用 regedit 运行此示例的效果,请检查以下键的值。 请注意,应用程序应避免在硬编码注册表路径中使用 Wow6432Node。
HKLM\SOFTWARE\Hello World
HKLM\SOFTWARE\Wow6432Node\Hello World
HKCR\CLSID\{00000000-0000-0000-0000-ABCD00000000}
HKCR\CLSID\{00000000-0000-0000-0000-ABCD000000000}\InprocServer32
HKCR\CLSID\{00000000-0000-0000-0000-ABCD000000000}\LocalServer32
HKCR\Hello HKCR\Wow6432Node\CLSID\{00000000-0000-0000-0000-ABCD00000000}
HKCR\Wow6432Node\CLSID\{00000000-0000-0000-0000-ABCD00000000}\InprocServer32
HKCR\Wow6432Node\CLSID\{00000000-0000-0000-0000-ABCD00000000}\LocalServer32
HKCR\Wow6432Node\Hello
相关主题
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈