Functions related to registry operations can be divided into categories such as opening the registry, closing the registry, reading item values, c adding item values, adding items, and deleting items.
Table 1 Windows Registry Basic items
|
Item Name |
Describe |
|
HKey_Classes_Root |
Is the HKEY_LOCAL_MACHINE\Software subkey that holds the application information for the open file |
|
HKey_Current_User |
is a subkey of HKEY_USERS, saving the current user's configuration information |
|
HKEY_Local_Machine |
Save the computer's configuration information for all users |
|
Hkey_users |
Save all user profiles that are loaded on the computer in an active manner |
|
Hkey_current_config |
To save hardware profile information for a computer |
By using the Win32API module and the Win32con module, Python can easily access the registry and open, close, add items, delete items, add, modify item values, and more.
1. Open the Registration form
RegOpenKey (Key, subkey, reserved, Sam)
RegOpenKeyEx (Key, subkey, reserved, Sam)
The parameters of the two function are the same. The parameters have the following meanings:
L Key: Must be the item listed in table 1.
L Subkey: the subkey to open.
L Reserved: Must be 0.
L SAM: Actions on open subkeys, including Win32con. Key_all_access, Win32con. Key_read, Win32con. Key_write, etc.
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>
such as Key=win32api. RegOpenKey (Win32con.hkey_current_user, ' software ', 0,win32con. Key_read)
Print key
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<
2. Close the Registration form
RegCloseKey (Key)
Its parameters are only one, meaning the following:
L Key: A handle to a registry key that has already been opened.
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>
such as Win32API. RegCloseKey (Key)
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<
3. Reading Item values
RegQueryValue (key,subkey) Read the default value of an item
RegQueryValueEx (Key,valuename) reads an item value
For RegQueryValue, the parameters have the following meanings:
L Key: A handle to a registry key that has been opened.
L Subkey: the subkey to manipulate.
For RegQueryValueEx, the parameters have the following meanings:
L Key: A handle to a registry key that has already been opened.
L ValueName: The name of the item value to read.
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>
Import Win32API
Import Win32con
# open "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer" entry
>>> key = Win32API. RegOpenKey (Win32con. HKEY_LOCAL_MACHINE,
' Software\\microsoft\\internet Explorer ', 0, Win32con. key_all_access)
>>> Win32API. RegQueryValue (Key, ') # reads the default value of the item
' # output is empty, indicating that its default value is not set
#读取项值名称为Version的项值数据, which is the version of Internet Explorer
(' 6.0.2900.2180 ', 1)
>>> Win32API. RegQueryInfoKey (key) # RegQueryInfoKey function Query the basic information of an item
(7, 128178812229687500L) # Returns the number of subkeys of an item, the number of item values, and the last modified time
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<
4. Setting the Item value
RegSetValueEx (Key,valuename,reserved,type,value) to modify or reset the entry value of an item in the registry. If the item value exists, the item value is modified, and if it does not exist, the item value is added.
RegSetValue (key,subkey,type,value) sets the default value of the item
For RegSetValueEx, the parameters have the following meanings:
L Key: The handle of the item to be set.
L ValueName: The name of the item value to set.
L Reserved: Reserved, can be set to 0.
L Type: The type of the item value.
L Value: The value you want to set.
For RegSetValue, the parameters have the following meanings:
L Key: A handle to an item that has already been opened.
L Subkey: The subkey that you want to set.
L Type: The type of the item value must be Win32con. REG_SZ.
L Value: The item value data, as a string.
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>
# will be "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer"
The default value is set to Python
>>> Win32API. RegSetValue (Key, "', Win32con. REG_SZ, ' Python ')
# set its "Version" to 7.0.2900.2180
>>> Win32API. RegSetValueEx (Key, ' Version ', 0,win32con. REG_SZ, ' 7.0.2900.2180 ')
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<
5. Adding and removing items
RegCreateKey (Key,subkey) Add an item to the registry
Regdeletekey (Key,subkey) deleting entries in the registry
The parameters have the same meaning, and the parameters are as follows:
L Key: A handle to a registry key that has already been opened.
Subkey: The subkey that you want to manipulate (add or remove).
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>
# Add Child "Python" to "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer"
>>> Win32API. RegCreateKey (Key, ' Python ')
# The handle of the newly created subkey
# Delete the subkey you just created "Python"
>>> Win32API. Regdeletekey (Key, ' Python ')
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<
6. Error handling
Importerror:no module named Win32API exception occurred
The actual need to install and own Python compatible win32all in http://starship.python.net/crew/mhammond/downloads/can be downloaded to fit your version, install; Remember that a restart is required to take effect.
Python modifies the registry