Accessing the 64-bit Registry from a 32-bit Windows Application
Challenge:
32-bit applications face difficulties accessing the 64-bit registry on 64-bit Windows systems. This issue often arises when accessing system information residing in the 64-bit registry, such as the path to a SQL Express instance, from a 32-bit unit test running on a 64-bit build server.
Solution:
Accessing the 64-bit Registry:
The RegistryView.Registry64
property provides the solution for accessing the 64-bit registry from a 32-bit application. The following code demonstrates this:
<code class="language-csharp">RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); RegistryKey sqlServerKey = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"); string sqlExpressKeyName = (string)sqlServerKey.GetValue("SQLEXPRESS");</code>
Accessing the 32-bit Registry:
Conversely, to access the 32-bit registry, use RegistryView.Registry32
:
<code class="language-csharp">RegistryKey localKey32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); RegistryKey sqlServerKey32 = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"); string sqlExpressKeyName = (string)sqlServerKey32.GetValue("SQLEXPRESS");</code>
Important Considerations:
RegistryView.Registry32
is the recommended and more direct approach.RegisteredOrganization
value to incorrectly return "Microsoft" when accessed from 32-bit code. The 64-bit code will return the correct organization.GetAllRegValueNames()
function allows retrieval of all key names and values, regardless of registry view (32-bit or 64-bit).??
) to gracefully handle potential null values.The above is the detailed content of How Can a 32-bit Application Access the 64-bit Registry in Windows?. For more information, please follow other related articles on the PHP Chinese website!