This article describes how to create a simple Python program for operating the Windows registry, including how to remotely modify the registry. For more information, see the following two methods to operate the registry using Python, the first is through the Python built-in module _ winreg; the other is the win32api module of Win32 Extension For Python, 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
Import _ winreg key = _ winreg. openKey (_ winreg. HKEY_CURRENT_USER, r "Software \ Microsoft \ Windows \ CurrentVersion \ Explorer") # obtain all key values of the key, because there is no way to obtain the number of key values, so we can only use this method to traverse try: I = 0while1: # The EnumValue method is used to enumerate key values, and EnumKey is used to enumerate sub-keys name, value, type = _ winreg. enumValue (key, I) print repr (name), I + = 1 TB T WindowsError: print # If you know the key name, you can also directly set the value to value, type = _ winreg. queryValueEx (key, "EnableAutoTray ")
1.2 create and modify
Import _ winreg key = _ winreg. openKey (_ winreg. HKEY_CURRENT_USER, r "Software \ Microsoft \ Windows \ CurrentVersion \ Explorer") # Delete key _ winreg. deleteKey (key, "Advanced") # Delete key value _ winreg. deleteValue (key, "IconUnderline") # Create a new key newKey = _ winreg. createKey (key, "MyNewkey") # add the key value _ winreg to the newly created key. 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:
import _winregkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows")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:
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 to directly access the 64-bit registry without redirection. the default parameter is win32con. KEY_WOW64_32KEY.