_winreg- Windows registry access


¥»¤å«á­±ªº¨ç¦¡©|¥¼§¹¥þ½Ķ§¹¦¨,¦Ó°ò¥»Æ[©À,«h¬OÀ³¥Î³o¨Ç¨ç¦¡©ÒÀ³¨ã³Æªº±`ÃÑ,½Ð¥ý°Ñ¦Ò§_«h·|µLªkÀH¤ß©Ò±ýªºµo´§¨ç¦¡ªº¥\¯à

¦p¦ó¦s¨úwindows¨t²Îªºµn¿ýÀÉ,§Ú¦bpython¤¤§ä¨ì¨â­Ó¥i¥Îªº¼Ò²Õ_winreg,¤Îwin32api,¥Ñ©ó_winreg¬O¤º«Øªº¼Ò²Õ,¦Ówin32api«h¬Owin32extned¤¤ªº¼Ò²Õ(¥²¶·¥t¥~¦w¸Ë),©Ò¥H§Ú«ØÄ³¨Ï¥Î_winreg¼Ò²Õ,¦ý¦b¨Ï¥Î¤§«e§Ú­Ì­n¥ý¤F¸Ñ¤@¤Uwinodwsªºµn¿ýÀÉ,¥H¤U¬Oµn¿ýÀɪºµe­±


§Ú­Ì¥i¥H¬Ý¨ì¦@¦³¤»­Ó¤W¼h¾÷½X
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
HKEY_CURRENT_CONFIG
HKEY_DYN_DATA
¦Óµøµ¡¤À¦¨¬O¤À¦¨¥ª¥k¨â¥b
¥ªÃä¬O¾÷½X,¥kÃä¬O¼Æ­È(¦A²Ó¤À¦WºÙ,¸ê®Æ)³o¬O«Ü­«­nªº,¥H¤U¨ç¦¡ªº¾Þ§@¤]¬O¤À¦¨³o¨âÃþ,¤£­n§Ë²V¤F
¦Ó¼Æ­È¤º¦³¨âºØ«¬ºA,¤å¦r(¹Ï¥Üab)¤Î¤G¦ì¤¸¼Æ¦r(¹Ï¥Ü0101)


¾Ú§Ú©Òª¾,©Ò¦³¦w¸Ëªºµ{¦¡¤j·§³£¬O©Ò©ñ¸mªº¦a¤è¦b
HKEY_LOCAL_MACHINE\Software\µ{¦¡¦WºÙ
¤U¹Ï¬Oµ{¦¡WHFCªºµe­± HKEY_LOCAL_MACHINE\Software\WHFC


¦³¤@¨Ç­«ÂI»Ý­n¸ÑÄÀ:_winreg¨Ï¥Î¨ìªº°Ñ¼Æ
  1. key-¤W¼h¾÷½X ,¦b¼Ò¦¡¤¤¤@¦A´£¨ì¨Ï¥Î¤v¶}±ÒªºKey,©Î¬OHKEY*¤§Ãþªº¦r¦ê,¦ý¬O¦ó¿×¤w¶}±ÒªºKEY©O?¸g§Ú´ú¸Õªºµ²ªGÀ³¸Ó,¬O­n¥ý¨Ï¥ÎOpenKey()³o­Ó¨ç¦¡,¶}±Ò¦Ó³oùتºKey ¬O¨Ï¥Î¨ä¦^¶Çªº«ü¼Ð
  2. sub_key-¤l¾÷½X(ª`·N¤£¥i¥H¥]§t¤W¹Ï¥kÃ䪺¼Æ­È)
  3. §A¥i¥H¨Ï¥Îªº¤º«Ø¾÷½X
  4. ¾÷½X
    _winreg.
    HKEY_CLASSES_ROOT
    _winreg.
    HKEY_CURRENT_USER
    _winreg.
    HKEY_LOCAL_MACHINE
    _winreg.
    HKEY_USERS
    _winreg.
    HKEY_CURRENT_CONFIG
    _winreg.
    HKEY_DYN_DATA

  5. ¾÷½XªºÀ³¥Î¤@¯ë¨ÓÁ¿¬Okey=OpenKey(),key ´N¬O«á­±­n¨Ï¥Îªº

  6. §Ú­Ì¥i¥H¬Ý¨ì¤U­±¦³¤@¨Ç¨ç¦¡¦³EX(OpenKeyEx,QueryValueEx,SetValueEx),³o¬O¬ÛÃö¨ì¼Æ­È³¡¥÷,¤§«e§Ú­Ì¦³¥Î´£¨ì¥÷¬°Áä­È¤Î¼Æ­È³¡¥÷,À³¥Î³¡¥÷¦³©Ò¤£¦P,¦pSetValue¥¦¬O¦b³]©w¤l¾÷½Xªº¹w³]­È,¦ÓSetValueEx«h¬O³]©wùØ­±ªº­È
  7. °£¤F¤W¼h¾÷½X¤§¥~¤l¾÷½X¤Î¤l¾÷½X¤ºªº¾÷½X,¦b¾Þ§@¤W¨Ã¨S¦³«ÜÄY®æªº¤À¬É,key,©Msub_key,¦b§A¨Ï¥ÎOpenKey()¶}±Ò®É,sub_key¦pªG¦³«ü©w,«h¶Ç¦^ªº«ü¼Ð,¦bµy«áÀ³¥Î´Nªí¥Ü¬O¬Û¹ï©ó³o­Ó¾÷½X¤§¤º

½d¨Ò:
¦bHKEY_LOCAL_MACHINE¤U«Ø¥ß¤@­Ó¾÷½Xcsc
import _winreg
handle_reg=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'')
_winreg.CreateKey(handle_reg,'csc')
#·s«Ø¤@­Ó¤l¾÷½Xªº¤º©w­È
_winreg.SetValue(aaa,'csc',REG_SZ,'this is a test')
#§R°£¤@­Ó¤l¾÷½Xcsc
_winreg.DeleteKey(aaa,'csc')
#¦b¾÷½X¤º«Ø¥ß¤@­Ó¼Æ­È,®æ¦¡¬°¤å¦r
_winreg.SetValueEx(aaa,'directory',_winreg.REG_SZ,'c:\windows\desktop')
_winreg.SetValueEX(aaa,'year',_winreg.REG_BINARY,2003)
#·|«Ø¤§«á­n·|¬d¸ß,¶Ç¦^¨â­Ó­È¤@­Ó¬O¨ä­È,¥t¤@­Ó«h¬O­Èªº®æ¦¡
_winreg.QueryValueEx(aaa,'year')
('2003',3)
#¦pªG§Úª¾¹D¦³¤@­Ó¤l¾÷½X¦s¦b,¦ý¬O­n¬d¸ß¨ä©Ò¦³ªº¤º®e(­È¤Î¤l¾÷½X)
_winreg.QueryInfoKey(aaa)
(0,4,0L)
#²Ä¤@­Ó¬O¨ä¤U¦³´X­Ó¤l¾÷½X,²Ä¤G­Ó¬O¥»¾÷½Xªº¼Æ­È¶µ¥Ø
#§Aª¾¹D¤F¦³´X­Ó¼Æ­È¶µ¥Ø¥i¥H¤@¤@¦CÁ|(¨ú±o)
_winreg.EnmuValue(aaa,0)
_winreg.EnmuValue(aaa,1)
_winreg.EnmuValue(aaa,2)
....
#¶Ç¦^
('year','2003',3)

#¬d¸ß¾÷½Xcscªº¹w³]­È
_winreg.QueryValue(aaa,'')

#


_winreg¥i¥Îªº¨ç¦¡¦p¤U:

CloseKey
(hkey)
Ãö³¬¤§«e¶}±Òªºregistry key.
ConnectRegistry(computer_name, key)
«Ø¥ß¤@­Ó³s½u¨ì¤@­Ó¥ý«e©w¸qªº predefined registry handle¦b¨ä¥¦ªº¹q¸£¤W,¨Ã¶Ç¦^¤@­Ó and returns a handle object

computer_name¬O»·ºÝ¹q¸£ªº¦WºÙ,¨ä®æ¦¡¬° r"\\computername".¦pªG¬ON0¨S¦³«h¬O¥»¦a¹q¸£.

¦^¶Çª«¥ó,¦pªG¨ç¦¡¦æ¿ù»~·|²£¥Í EnvironmentError¿ù»~

CreateKey(key, sub_key)
«Ø¥ß¤@­Ó¾÷½X'¦^¶Çª«¥ó

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

sub_key ¬O§Ú­Ì­n«Ø¥ßªº¤l¾÷½X,¨ä¤¤«Ø¥ßªº¾÷½X¬O­ìkey/sub_key,¦pªG¥ý«e¬O©w¸q¶}±Ò¤Fkey/subkey,«hsub_key¥i¥H¬ONone. ¦b¨ººØ±¡ªp¤U,In that case, the handle returned is the same key handle passed in to the function.

¦pªGkey ­È¤w¸g¦s¦b,³o­Ó¨ç¦¡«h·|¶}±Ò¦s¦bªº¾÷½X

¦^¶Çªº­È¬O¤@­Ó¶}±ÒÁä­Èªº handle. ¦pªG¥¢±Ñ·|²£¥ÍEnvironmentError.

DeleteKey(key, sub_key)
§R°£«ü©wªºkey.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

sub_key ¬O¤@­Ó¦r¦ê¥²¶·¬Okey¤Uªº¤lÁä.³o­Ó­È¤£¥i¥H¬OªÅªº(None), ¨Ã¥B³o­Ó key¥i¯à¨S¦³subkeys.

³o­Ó¤èªkµLªk§t¦³subkeysªºkey.

¦pªG°õ¦æ¦¨¥\,¾ã­Ókey,¥]§t¤F¥¦ªº­È,·|³Q²¾°£.¦pªG¥¢±Ñ«h·|²£¥Í EnvironmentError.

DeleteValue(key, value)
¥Ñµn¿ýÁ䤤²¾°£¤@­Ó¦WºÙ©M­È.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

value ¬O¤@­Ó¦r¦ê¥Î¨Ó«ü¥X­n²¾°£ªº¼Æ­È.

EnumKey(key, index)
¦CÁ|¤@­Ó¶}±Òªºkeyªº¤l¾÷½X,¶Ç¦^¤@­Ó¦r¦ê.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

index ¬O¤@­Ó¾ã¼Æ¨Óªí¥Ükey¬O¦^¶Çªº­È.

³o­Ó¨ç¦¡¶Ç¦^¨ä¤¤¤@­Ó¤l¾÷½X,¨C¤@¦¸³Q©I¥s. It is typically called repeatedly until an EnvironmentError exception is raised, indicating, no more values are available.

EnumValue(key, index)
¦CÁ|¤@­Ó¶}±Ò¾÷½Xªº­È,¶Ç¦^¤@­Ó°}¦C.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

index¬O¤@­Ó¾ã¼Æ that identifies the index of the value to retrieve.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly, until an EnvironmentError exception is raised, indicating no more values.

µ²ªG¬O¶Ç¦^¤T­Ó¤l¶µ¥Ø:

Index  ·N¸q
0 ¤@­Ó¦r¦ê«ü¥Xvalue name
1 ¤@­Óª«¥ó¥]¯dvalue data, and whose type depends on the underlying registry type
2 ¤@­Ó¾ã¼Æ«ü¥Xthe type of the value data

FlushKey(key)
¼g¦^©Ò¦³ªºWrites all the attributes of a key to the registry.

kkey¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

¤£¦b»Ý­n©I¥sRegFlushKey¨Ó§ïÅܤ@­Ó¾÷½X. Registry changes are flushed to disk by the registry using its lazy flusher. Registry changes are also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey() method returns only when all the data has been written to the registry. An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.

¦pªG§A¤£ª¾¹DFlushKey() call is required, it probably isn't.

RegLoadKey(key, sub_key, file_name)
«Ø¥ß¤@­Ó¤l¾÷½X¦b«ü©wªº¾÷¶Ü¨ÃÀx¦sµn¿ý¸ê®Æ¥Ñ«ü©wªºÀɮרì¨ì¤l¾÷½X.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

sub_key is a string that identifies the sub_key to load

file_name is the name of the file to load registry data from. This file must have been created with the SaveKey() function. Under the file allocation table (FAT) file system, the filename may not have an extension.

A call to LoadKey() fails if the calling process does not have the SE_RESTORE_PRIVILEGE privilege. Note that privileges are different than permissions - see the Win32 documentation for more details.

If key is a handle returned by ConnectRegistry(), then the path specified in fileName is relative to the remote computer.

The Win32 documentation implies key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree. This may or may not be true.

OpenKey(key, sub_key[, res = 0][, sam = KEY_READ])
¶}±Ò¤@µ¹©wªºkey(¤W¼h¾÷½X), ¦^¦s¤@­Óª«¥ó

key=¤º«Ø¾÷½X

sub_key ¬O¤l¾÷½X

res ¬O¤@­Ó«O¯d¦r¤º©w0.

sam ¬O¤@­Ó¾ã¼Æ¨Ó³]©w¦s¨ú¼Ò¦¡,¤º©w¬° KEY_READ

¶Ç¦^ª«¥ó
OpenKeyEx()
³o­Ó¨ç¦¡¬O¥ÑOpenKeyEx() is provided via OpenKey(), by the use of default arguments.
QueryInfoKey(key)
¶Ç¦^keyªº¤T­Ó­È(°}¦C)tuple.

key OpneKey()©Ò¶Ç¦^¨Óªºª«¥ó.

¤T­Ó­Èªº·N¸q¦p¤U:

¶¶§Ç
·N¸q
0 ¤lÁ䪺¼Æ¥Ø
1 ¥»Á䪺­È.
2 0L

QueryValue(key, sub_key)
Retrieves the unnamed value for a key, as a string

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

sub_key is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key.

Values in the registry have name, type, and data components. This method retrieves the data for a key's first value that has a NULL name. But the underlying API call doesn't return the type, Lame Lame Lame, DO NOT USE THIS!!!

QueryValueEx(key, value_name)
Retrieves the type and data for a specified value name associated with an open registry key.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

value_name is a string indicating the value to query.

The result is a tuple of 2 items:

Index  ·N¸q
0 The value of the registry item.
1 An integer giving the registry type for this value.

SaveKey(key, file_name)
Saves the specified key, and all its subkeys to the specified file.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

file_name ¬O­nÀx¦s¨ìªºÀɮצWºÙ. ³o­ÓÀɮפ£¥i¥H¤w¸g¦s¦b.¦pªGÀɦW¥]§t¤F°ÆÀɦW,¥¦¤£¥i¥H³Q¨Ï¥Î¦bÀÉ®×°t¸mªí(FAT)ÀÉ®× file systems by the LoadKey(), ReplaceKey() or RestoreKey() methods.

¦pªGkey ¥X¦b»·ºÝªº¹q¸£,³o­Ó¸ô®|´y­z¥Ñfile_name ¬OÃö³s¨ì»·ºÝªº¹q¸£. ©I¥s³oºØ¤èªk¥²¶·°õ¦æSeBackupPrivilege security privilege. Note that privileges are different than permissions - see the Win32 documentation for more details.

This function passes NULL for security_attributes to the API.

SetValue(key, sub_key, type, value)
Associates a value with a specified key.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

sub_key is a string that names the subkey with which the value is associated.

type ¥²¶·¬OREG_SZ, ·N¨ý¥u¦³¦r¦ê¬O³Q¤ä´©ªº.¦Ó¨Ï¥Î SetValueEx()¨ç¦¡«h¤ä´©§Oªº¸ê®Æ«¬ºA.

value is a string that specifies the new value.

If the key specified by the sub_key parameter does not exist, the SetValue function creates it.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

SetValueEx(key, value_name, reserved, type, value)
Stores data in the value field of an open registry key.

key¤w¸g¶}±Òªºkey, ©Î¬O¥ý«e©w¸qªº HKEY_* ±`¼Æ¤§¤@.

sub_key is a string that names the subkey with which the value is associated.

type is an integer that specifies the type of the data. This should be one of the following constants defined in this module:

±`¼Æ
·N¸q
REG_BINARY ¤G¦ì¤¸¸ê®Æin any form.
REG_DWORD ¤@­Ó32-¦ì¤¸ªº¼Æ¦r.
REG_DWORD_LITTLE_ENDIAN ¤@­Ó32-¦ì¤¸¼Æ¦rin little-endian format.
REG_DWORD_BIG_ENDIAN A 32-bit number in big-endian format.
REG_EXPAND_SZ Null-terminated string containing references to environment variables ("%PATH%").
REG_LINK A Unicode symbolic link.
REG_MULTI_SZ A sequence of null-terminated strings, terminated by two null characters. (Python handles this termination automatically.)
REG_NONE ¥¼©w¸qªº­È®æ¦¡.
REG_RESOURCE_LIST A device-driver resource list.
REG_SZ A null-terminated string.

reserved can be anything - zero is always passed to the API.

value is a string that specifies the new value.

This method can also set additional value and type information for the specified key. The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

To open the key, use the CreateKeyEx() or OpenKey() methods.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.