Simple Python program Authoring tutorial for manipulating the Windows registry

Source: Internet
Author: User
There are two ways to manipulate the registry through Python, the first of which is through Python's built-in module _winreg, and the other is Win32 Extension for Python's Win32API module, but requires additional installation. Here are some of the demo codes for _winreg and Win32API.

1, _winrg

Refer to the official reference documentation:

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

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

1.1 Read

Import _winreg key = _winreg. Openkey (_winreg. Hkey_current_user,r "Software\microsoft\windows\currentversion\explorer")  #获取该键的所有键值, because there is no way to get the number of key values, So it can only be traversed in this way try:  i = 0while1: #EnumValue方法用来枚举键值, EnumKey is used to enumerate sub-keys     name, value, type = _winreg. Enumvalue (key, i) print repr (name),    i +=1 except Windowserror:print  #如果知道键的名称, can also be directly evaluated as value, type = _winreg. QueryValueEx (Key, "Enableautotray")

1.2 Creating modifications

Import _winreg key = _winreg. Openkey (_winreg. Hkey_current_user,r "Software\microsoft\windows\currentversion\explorer") #删除键_winreg. DeleteKey (Key, "advanced") #删除键值_winreg. DeleteValue (Key, "iconunderline") #创建新的键newKey = _winreg. CreateKey (Key, "Mynewkey") #给新创建的键添加键值_winreg. SetValue (NewKey, "ValueName", 0, "valuecontent")

1.3 Accessing the Remote registry

1 #第二参数必须是HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE and other pre-defined values, get the key to return to the operation of the
2 key = _winreg. Connectregisty ("IP address or machine name", _winreg. HKEY_CURRENT_USER)

2, Win32API

Win32API and _winreg method is very similar, generally more than a prefix "Reg", the use of the same method is basically the same, here does not give a specific demo code.

For general applications, the use of _winreg is sufficient, but _winreg has a problem, if Python is 32-bit, and is running on a 64-bit operating system, there is a small problem, because the operating system will be redirected to the registry, The 32-bit program cannot access the registry for a 64-bit application. This problem was not resolved before Python2.6, and the problem was fixed in the form of patches in Python2.7 (http://bugs.python.org/issue7347).

Illustrate the problem with an example, assuming that our operating system is 64-bit, and then install the 32-bit Python on it, look at the following Python code:

Import _winregkey = _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 under 64-bit Windows operating systems, 64-bit programs and 32-bit programs are separated, and 32-bit applications operate on the registry (read, write) are redirected to Wow6432node, and the behavior of the 64-bit applications accessing the registry does not change. So if our 32-bit application does have access to the registry used by the 64-bit program, there's a problem. Due to the problem of _winreg itself, it does not support this situation very well, there are some bugs in the encapsulation of Windows API, this time 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)

Above the Win32con. Key_wow64_64key means direct access to the 64-bit registry, not redirection, and the default parameter is Win32con. Key_wow64_32key.

  • 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.