Example code for C # 32-bit programs accessing the 64-bit registry

Source: Internet
Author: User
   my last article has explained the difference between "32-bit programs and 64-bit programs reading \ Writing the registry on 64-bit platforms", and then you will answer a question left in the previous article: how the 32-bit program accesses the 64-bit system registry (that is, the registry location accessed by the 64-bit program).

We already know:

①: The native mode 64-bit program runs in native mode, and the access keys and values stored in the following registry subkey are: HKEY_LOCAL_MACHINE\Software

The ②:32-bit program runs in WOW64 mode, and the access keys and values are stored in the following registry subkey: Hkey_local_machine\software\wow6432nod

Then to implement 32 for the program to access 64-bit registry information, but also know the following concepts: 1: File system steering. 2: Registry redirect (Turn). 3: Registry Reflection.

    ①: File System Steering

A 32-bit process cannot load a 64-bit dll,64-bit process nor can a 32-bit DLL be loaded. The system directory for Windows contains all installed applications and their DLL files, according to the rules we have described,

It should be divided into directories for 64-bit applications and directories for 32-bit applications. If this is not the case, we will not be able to distinguish between 32-bit and 64-bit DLL files. For 64-bit applications, their files are typically

In%windir%\system32 and%programfiles% (e.g., c:\program Files). For 32-bit applications, their files are typically%windir%\syswow64 and

C:\program files (x86) below. If we use 32-bit programs to access%windir%\system32, whether we use hard coding or otherwise, the system will automatically give us

Turn to the%windir%\syswow64 below. This turn is turned on for each 32-bit application by default. But this shift is not always needed for us. Then we can

C # calls the relevant API to turn off and turn on the switch. There are 3 commonly used functions:

Wow64disablewow64fsredirection (Turn off system steering),

Wow64revertwow64fsredirection (Turn on system turn),

Wow64enablewow64fsredirection (Turn on system turn).

However, Wow64enablewow64fsredirection is not reliable when nested, so it is common to use the wow64revertwow64fsredirection above to turn on the file system.

Function. In C #, we can call these two functions directly using DllImport.

    ②: Registry redirection (steering)

To support the 32-bit and 64-bit COM registration and program coexistence states, the WOW64 subsystem provides another view of the registry that is used by 32-bit programs. Using the registry in the WOW64 subsystem

Redirects the interception of a bit-level registry call. Registry redirection can also ensure that a registry call is directed to the correct branch in the registry.
A registry call to a 64-bit program that accesses the HKEY_LOCAL_MACHINE\Software registry subkey when we run a program on a new program or a Windows version x64 computer

Do not redirect. WOW64 intercepts the registry calls from the 32-bit program to HKEY_LOCAL_MACHINE\Software, and then redirects them to the

Hkey_local_machine\software\wow6432node the child key. By redirecting only 32-bit program calls, WOW64 ensures that programs always write to the appropriate registry subkey.

Registry redirection does not require program code modification, and this process is transparent to the user.

   ③: Registry Reflection

Reflection enables the existence of two identical registries to support simultaneous native and WOW64 operations of physical copies,

Opening the 64-bit section of the registry at all times and registry reflection provides a real-time method for accommodating 32-bit.


Simply understand these, the following is the specific implementation steps:

Turn off 64-bit (file system) operation steering

Get handle to Operation key value

Turn off registry steering (disables registry reflection for specific items)

Gets the key value of the access

Turn on registry turn (turn on registry reflection for specific items)

Turning on 64-bit (file system) operation


"Note: Since we are using DllImport in the program, we introduce namespaces: System.Runtime.InteropServices"

See the code example below

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using Microsoft.Win32;  6 using System.Runtime.InteropServices; 7 8 Namespace Operateregistrationtable 9 {Ten class Programe-one {static void Main (string[] args) 1 3 {String myparentkeyname = "HKEY_LOCAL_MACHINE"; string mysubkeyname = @ "software\ Ericsun\mytestkey "; string mykeyname = "MyKeyName"; *. String value = String. Empty; Value = Utility.get64bitregistrykey (Myparentkeyname, Mysubkeyname, MyKeyName); Console.WriteLine ("The value is: {0}", value); 32-bit program read/write 64 registry of the public class Utility TR HKEY_CLASSES_ROOT = (UINTPTR) 0x80000000; static UINTPTR HKEY_CURRENT_USER = (UINTPTR) 0x80000001; Static UINTPTR HKEY_LOCAL_MACHINE = (UINTPTR) 0x80000002; StaTic UIntPtr hkey_users = (UIntPtr) 0x80000003; static UIntPtr Hkey_current_config = (UIntPtr) 0x80000005;          33 34//off 64-bit (file system) operation steering [DllImport ("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = True)] 36 public static extern bool Wow64disablewow64fsredirection (ref IntPtr PTR);         37//Turn on 64-bit (file system) operation to [DllImport ("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 39     public static extern bool Wow64revertwow64fsredirection (IntPtr ptr);         40 41//Get operation key value handle [DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 43                                   public static extern uint RegOpenKeyEx (UIntPtr hKey, String lpsubkey, uint uloptions, int samdesired, out IntPtr phkresult);         44//Turn off Registry steering (disables registry reflection for specific items) [DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = True)] 46 public static extern long Regdisablereflectionkey (IntPtr HKey);47//Enable registry turn (turn on registry reflection for specific items) [DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 49 public static extern long Regenablereflectionkey (IntPtr HKey); 50//Gets the key value (that is, the value of the key object as marked by the key value handle). DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = True                                                   )] RegQueryValueEx private static extern int (IntPtr HKey, string lpvaluename, int lpreserved, 53                                                   Out uint Lptype, System.Text.StringBuilder lpdata, 54 ref UINT Lpcbdata);             The private static UIntPtr Transferkeyname (String keyName), (KeyName) 59                 {case "HKEY_CLASSES_ROOT": HKEY_CLASSES_ROOT return; 62 Case "HKEY_CURRENT_USER": HKEY_CURRENT_USER return; "HKEY_LOCAL_MACHINE": Hkey_loc returnAl_machine; "HKEY_USERS": HKEY_USERS return; "Hkey_current_config": hkey_current_config return; The HKEY_CLASSES_ROOT return; Get64bitregistrykey (String parentkeyname, String subkeyname, String Keyna             Me) {Key_query_value int = (0x0001); Key_wow64_64key int = (0x0100); 79 int key_all_wow64 = (Key_query_value | Key_wow64_64key);                 Bayi Try 82 {83//Convert the Windows Registry primary key name to an unsigned shape handle (related to platform 32 or 64 bits) 84 UIntPtr HKey = Transferkeyname (parentkeyname); 85 86//declares the handle that will get the key value. IntPtr Phkey = IntPtr.Zero; 88 89//record read to the key value of StringBuilder result = new StringBuilder ("". PadLeft (1024)); resultsize UINT = 1024; Lpty UINTPE = 0; 93 94//Close File system turn to IntPtr oldwow64state = new IntPtr (); if (Wow64disablewow64fsredirection (ref oldwow64state)) 97 {98//Get The handle to the KEY value is RegOpenKeyEx (HKey, subkeyname, 0, Key_all_wow64, out Phkey); 100 101/                     /Turn off Registry steering (disable registry reflection for specific items) 102 Regdisablereflectionkey (Phkey); 103 104//Get access to the key value 105 RegQueryValueEx (Phkey, KeyName, 0, out lptype, result, ref resultsize); 106 107//Open Registration Table steering (Turn on registry reflection for specific items) 108 Regenablereflectionkey (Phkey); 109}110 111//Open File system Wow64revertwow64fsredirection (oldwow64state); 113 114//Return key value Turn result. ToString ().             Trim ();}117 catch (Exception ex) 118 {119 Return null;120 }121}122 123         #endregion124}125} 

The three parameters of the Get64bitregistrykey function represent: The primary key name (such as: HKEY_LOCAL_MACHINE, etc.), the subkey name, the key name, and the value returned by key (the key value of the 64-bit system registry). Using the method above, it is possible to access the 64-bit system registry (that is, the registry location accessed by 64-bit programs) with 32 programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.