Python operation Registry

Source: Internet
Author: User
There are two ways to operate the Registry through Python: the first is through Python's built-in module _ winreg; the other is Win32 Extension For Python's win32api module, but additional installation is required. Here we mainly provide Demo code for _ winreg and win32api.
 
1、 _winrg
You can refer to the official reference documents:
http://docs.python.org/library/_winreg.html
http://www.python.org/doc/2.6.2/library/_winreg.html
 
Read 1.1
 
1 import _ winreg
2
3 key = _ winreg. OpenKey (_ winreg. HKEY_CURRENT_USER, r "Software \ Microsoft \ Windows \ CurrentVersion \ Explorer ")
4
5 # obtain all the key values of the key. Because there is no way to obtain the number of key values, you can only use this method for traversal.
6 try:
7 I = 0
8 while1:
9 # The EnumValue method is used to enumerate key values, and the EnumKey is used to enumerate subkeys.
10 name, value, type = _ winreg. EnumValue (key, I)
11 print repr (name ),
12 I + = 1
13 TB WindowsError:
14 print
15
16 # If you know the key name, you can also directly set the value
17 value, type = _ winreg. QueryValueEx (key, "EnableAutoTray ")

 

1.2 create and modify

 

1 import _ winreg
2
3 key = _ winreg. OpenKey (_ winreg. HKEY_CURRENT_USER, r "Software \ Microsoft \ Windows \ CurrentVersion \ Explorer ")
4
5 # delete key
6 _ winreg. DeleteKey (key, "Advanced ")
7
8 # delete a key value
9 _ winreg. DeleteValue (key, "IconUnderline ")
10
11 # create a new key
12 newKey = _ winreg. CreateKey (key, "MyNewkey ")
13
14 # Add a key value to the newly created key
15 _ winreg. SetValue (newKey, "ValueName", 0, "ValueContent ")

 

1.3 access the Remote Registry

 

1 # The second parameter must be HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, and other pre-defined values. After obtaining the returned key, you can perform operations.
2 key = _ winreg. ConnectRegisty ("IP address or machine name", _ winreg. HKEY_CURRENT_USER)

 

 

2. win32api

The methods of win32api and _ winreg are very similar. Generally, there is an additional prefix "Reg". The usage is basically the same. Here we will not provide the specific Demo code.

For general applications, using _ winreg is sufficient, but _ winreg has a problem. If Python is 32-bit, in addition, when running on a 64-bit operating system, there will be a small problem. Because the operating system will redirect the registry, 32-bit programs cannot access the registry of the 64-bit application. This issue was not resolved before Python2.6 and was fixed in the form of patches in Python2.7 (http://bugs.python.org/issue7347 ).

Let's use an example to illustrate this problem. Assume that our operating system is 64-bit, And a 32-bit Python is installed on it. See the following Python code:

 

1 import _ winreg
2
3 key = _ winreg. OpenKey (_ winreg. HKEY_LOCAL_MACHINE, r "Software \ Microsoft \ Windows ")
4
5 newKey = _ winreg. CreateKey (key, "MyNewkey ")

 

The preceding Code does not create the following keys as expected:

"HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows"

Instead, the following keys are created:

"HKEY_LOCAL_MACHINE \ Software \Wow6432Node\ Microsoft \ Windows"

In a 64-bit windows operating system, the 64-bit program is separated from the 32-bit program registry, and the 32-bit application operates the Registry (read and write) will be redirected to the Wow6432Node, And the 64-bit application's access to the registry remains unchanged. Therefore, if our 32-bit application really needs to access the registry used by the 64-bit program, it will be a problem. Due to the problem of _ winreg itself, it does not fully support this situation, and there are some bugs in windows api encapsulation. In this case, we need to use the win32api module.

The following code is modified using the method provided by win32api:

 

1 import win32api
2
3 import win32con
4
5 key = win32api. RegCreateKeyEx (win32con. HKEY_LOCAL_MACHINE, r "Software \ Microsoft \ Windows", win32con. WRITE_OWNER | win32con. KEY_WOW64_64KEY | win32con. KEY_ALL_ACCESS)
6 win32api. RegSetValueEx (key, "MyNewkey", 0, win32con. REG_SZ, keyValue)

 

The above win32con. KEY_WOW64_64KEY means to directly access the 64-bit registry without redirection. The default parameter is win32con. KEY_WOW64_32KEY.

 

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.