Simple Python program making tutorial for operating Windows registry

  • 2020-05-05 11:28:27
  • OfStack

There are two ways to manipulate the registry through Python. The first is through Python's built-in module _winreg. Another option is the win32api module for Win32 Extension For Python, but requires additional installation. Here I mainly give some Demo codes for _winreg and win32api.

 

1, _winrg

You can refer to the official reference document:

http://docs.python.org/library/_winreg.html

http://www.python.org/doc/2.6.2/library/_winreg.html

 

1.1 reads  
 


import _winreg
 
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer")
 
 # Gets all of the key values for the key, and because there is no way to get the number of key values, it is the only way to iterate 
 try:
  i = 0
while1:
#EnumValue Method to enumerate key values, EnumKey Used to enumerate child keys 
     name, value, type = _winreg.EnumValue(key, i)
print repr(name),
    i +=1
 except WindowsError:
print
 
 # If you know the name of the key, you can also evaluate it directly 
 value, type = _winreg.QueryValueEx(key, "EnableAutoTray")

1.2 create modify
 


import _winreg
 
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer")
 
# The delete key 
_winreg.DeleteKey(key, "Advanced")
 
# The delete key value 
_winreg.DeleteValue(key, "IconUnderline")
 
# Create a new key 
newKey = _winreg.CreateKey(key,"MyNewkey")
 
# Add a key value to the newly created key 
_winreg.SetValue(newKey,"ValueName",0,"ValueContent")

1.3 access to the remote registry
 
1 # the second parameter must be the predefined values of HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, etc. After getting the returned key,
can be operated 2 key = _winreg.ConnectRegisty ("IP address or machine name ", _winreg_CURRENT_USER)

2, win32api

win32api and _winreg are similar in method. Generally, the prefix "Reg" is added. The usage method is basically the same.

For general applications, _winreg is sufficient, but _winreg has a problem. If Python is 32-bit and runs on a 64-bit operating system, there is a small problem. Because the operating system redirects the registry, 32-bit programs cannot access the registry of 64-bit applications. In Python2. 6 did not solve this problem before, in Python2. 7 through patches in the form of a fixed the problem (http: / / bugs. python. org/issue7347).

To illustrate this problem, let's say our operating system is 64-bit, and then we install 32-bit Python on it. See Python:
 


import _winreg
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows")
newKey = _winreg.CreateKey(key,"MyNewkey")

The execution of the above code does not create the following key as expected :

"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"

Instead, the following key is created:

"HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows"

Because in the 64-bit windows operating system, the 64-bit program and the 32-bit program registry are separate, the 32-bit application's operations on the registry (read and write) are redirected to Wow6432Node, while the 64-bit application's access to the registry remains unchanged. So if our 32-bit application does want to access the registry used by the 64-bit program, we have a problem. Due to _winreg itself, it does not fully support this situation. There are some bug in the encapsulation of windows api, which requires the win32api module.

Here's how to modify the code above using the method provided by win32api:
 


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

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


Related articles: