C # What is the difference between a 32-bit program and a 64-bit program reading/writing a registry?

Source: Internet
Author: User

It is easy to use C # To implement the reading and writing of the Registry. I will not explain it in detail here.

Using C # To operate the registry, the two main functions are as follows:ProgramNote: To introduce the Microsoft. Win32 namespace ):

1: Read the key value --> registry. localmachine.Opensubkey(".. Key Path... ", true). The meaning of the 2nd Boolean parameters here is: indicates whether the opened key value can be changed (that is, whether setvalue () can be used to assign values to the key ), then, call the getvalue () method to read the key value.

2: Write key value --> registry. localmachine.Createsubkey(".. Key path..."), and then call setvalue () to write the key value.

The difference between a 32-bit program and a 64-bit program reading and writing the registry on a 64-bit platform is explained here. [Note: The 32-bit program is --> the build platform target is x86; 64-bit program --> build the platform target is x64, and vs2010 is the x86 compiling environment by default (that is, 32-bit )]

Briefly repeat the theoretical basis: in order to allow 32-bit programs to run on 64 operating systems without any modification, Microsoft has added a very important wow64 subsystem to implement this function, wow64 is short for Windows-32-on-Windows-64. In general, wow64 is a set of dynamic link libraries based on user mode, it can translate the commands issued by 32-bit applications into a format acceptable to 64-bit systems, that is: the Wow layer handles transactions such as switching the processor between 32-bit and 64-bit modes and simulating 32-bit systems.

The characteristics of 32-bit and 64-bit are as follows:File System and Registry.

File System: 32-bit processes cannot load 64-bit DLL, and 64-bit processes cannot load 32-bit DLL.

Registry: to prevent registry key conflicts, the 64-bit registry information is divided into two parts. Some of them are specially accessed by 64-bit systems (that is, 64-bit programs), and the other is specially accessed by 32-bit systems (that is, 32-bit programs), which are placed under wow6432node. (The wow6432node node exists under HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER)

Now that we know that the registry information is divided into two parts, we can think that when we use a 32-bit program and a 64-bit program to operate the registry, we will operate the registry information in different locations. The following example demonstrates this statement.

  1   Using  System;
2   Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using Microsoft. Win32;
6
7 Namespace Operateregistrationtable
8 {
9 Class Programe
10 {
11 Static Void Main ( String [] ARGs)
12 {
13 Operatingregistrykey ();
14 }
15
16 Public Static Void Operatingregistrykey ()
17 {
18 String KeyValue = String . Empty;
19 Try
20 {
21 // Write information to the Registry
22 Using (Registrykey key = Registry. localmachine. opensubkey ( @" Software \ ericsun \ mytestkey " , True ))
23 {
24 If (Key = Null )
25 {
26 Using (Registrykey mykey = Registry. localmachine. createsubkey ( @" Software \ ericsun \ mytestkey " ))
27 {
28 Mykey. setvalue ( " Mykeyname " , " Hello ericsun. " + Datetime. Now. tostring ());
29 }
30 }
31 Else
32 {
33 Key. setvalue ( " Mykeyname " , " Hello ericsun. " + Datetime. Now. tostring ());
34 }
35 }
36
37 // Read Registry Information
38 Using (Registrykey currentkey = Registry. localmachine. opensubkey ( @" Software \ ericsun \ mytestkey " , False ))
39 {
40 If (Currentkey = Null )
41 {
42 Console. writeline ( " Hello ericsun, the key you tried to open doesn't exist. " );
43 }
44 Else
45 {
46 KeyValue = Currentkey. getvalue ( " Mykeyname " ). Tostring ();
47 Console. writeline ( " The key value is: {0} " , KeyValue );
48 }
49 }
50 }
51 Catch (Exception ex)
52 {}
53 }
54 }
55 }
56

Compile and run this program on the x86 (32-bit) platform. You will find that the sub-key: ericsun \ mytestkey is created under the wow6432node node in the registry, in addition, the value of the key mykeyname is filled (time is used to distinguish its value), but this ericsun is not found in the first layer of child nodes of software. It can be determined that the 32-bit program operates the registry information under the wow6432node node.

If we do not modify this program and compile and run it on the x64 (or any CPU) platform, the ericsun node is created in the first subnode of the software node of the Registry (and the corresponding registry information is created under this node ), then, when we use the same program to read the registry, we will find that the places they read are different (based on the time information in the program)

Summary: X64, (or any CPU) the program under platform will operate 64-bit machines to store the registry information at the Registry location, the program under X86 platform will operate on 32-bit machines to store the registry information at the Registry location (that is, the registry information under the wow6432node node)

If you want to know how to use a 32-bit program to operate the registry information of 64 machines, please follow my next articleArticle: Http://www.cnblogs.com/mingmingruyuedlut/archive/2011/01/21/1941225.html

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.