我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用winreg.ConnectRegistry()。
def CoCreateInstanceC2R (self, store, reg, clsid, iid) : # Ugly code to find DLL in C2R version of COM object and get a COM # object despite the fact that COM doesn't handle C2R try: # Get DLL to load from 2R register aReg = winreg.ConnectRegistry(None, store) aKey = winreg.OpenKey(aReg, reg, 0, winreg.KEY_READ | winreg.KEY_WOW64_64KEY) dummy_n, IconvDLL, dummy_t = winreg.EnumValue(aKey, 0) winreg.CloseKey(aKey) winreg.CloseKey(aReg) # Create OLE object from DLL IconvOLE = ctypes.OleDLL(IconvDLL) # Get COM Instance from OLE clsid_class = uuid.UUID(str(clsid)).bytes_le iclassfactory = uuid.UUID(str(pythoncom.IID_IClassFactory)).bytes_le com_classfactory = ctypes.c_long(0) IconvOLE.DllGetClassObject(clsid_class, iclassfactory, ctypes.byref(com_classfactory)) MyFactory = pythoncom.ObjectFromAddress(com_classfactory.value, pythoncom.IID_IClassFactory) return MyFactory.CreateInstance (None, str(iid)) except: return None
def enumRegKeyValues(self, key): """List all values of a specified key in the windows registry @param key: The registry key to check. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion" @type key: string @return: An array of tupples containing the name of each value, the data of the value and it's type @rtype: tupple(string, WinRegValueType) """ hkey, key = self._getHiveAndKey(key) aReg = reg.ConnectRegistry(None, hkey) aKey = reg.OpenKey(aReg, key) result = [] index = 0 # The function EnumValue() 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. while True: try: valueName, valueData, valueType = reg.EnumValue(aKey, index) result.append((valueName, valueData, WinRegValueType.findByIntegerValue(valueType))) index += 1 except EnvironmentError: return result
def enumRegKeySubkeys(self, key): """List all sub-keys of a specified key in the windows registry @param key: The registry key to check. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion" @type key: string """ hkey, key = self._getHiveAndKey(key) aReg = reg.ConnectRegistry(None, hkey) aKey = reg.OpenKey(aReg, key) result = [] index = 0 # The function EnumKey() 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. while True: try: subkey = reg.EnumKey(aKey, index) result.append(subkey) index += 1 except EnvironmentError: return result
def OutlookPath(): """Function to retrieve the path to Outlook from the registry""" aReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE") # prepend unused variables with "dummy_" to keep PyLint happy dummy_n, v, dummy_t = winreg.EnumValue(aKey, 0) winreg.CloseKey(aKey) winreg.CloseKey(aReg) return v
def registryHasKey(self, key): """Check if the windows registry has the specified key @param key: The registry key to check. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion" @type key: string """ try: hkey, key = self._getHiveAndKey(key) aReg = reg.ConnectRegistry(None, hkey) aKey = reg.OpenKey(aReg, key) return True except EnvironmentError: return False
def getValueFromRegKey(self, key, valueName): """Retrieves a value for a key @param key: The registry key that holds the value to get. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion" @type key: string @param valueName: The name of the value to retrieve @type valueName: string @return: A tupple containing the data of the value with the specified name and it's type @rtype: tupple(string, WinRegValueType) """ hkey, key = self._getHiveAndKey(key) aReg = reg.ConnectRegistry(None, hkey) aKey = reg.OpenKey(aReg, key) value, int_type = reg.QueryValueEx(aKey, valueName) return value, WinRegValueType.findByIntegerValue(int_type)
def deleteRegKey(self, key): """Deletes a key from the Windows Registry @param key: The registry key that should be deleted. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion" @type key: string """ hkey, key = self._getHiveAndKey(key) aReg = reg.ConnectRegistry(None, hkey) reg.DeleteKey(aReg, key)
def get_localzone_name(): # Windows is special. It has unique time zone names (in several # meanings of the word) available, but unfortunately, they can be # translated to the language of the operating system, so we need to # do a backwards lookup, by going through all time zones and see which # one matches. handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) TZLOCALKEYNAME = r'SYSTEM\CurrentControlSet\Control\TimeZoneInformation' localtz = winreg.OpenKey(handle, TZLOCALKEYNAME) keyvalues = valuestodict(localtz) localtz.Close() if 'TimeZoneKeyName' in keyvalues: # Windows 7 (and Vista?) # For some reason this returns a string with loads of NUL bytes at # least on some systems. I don't know if this is a bug somewhere, I # just work around it. tzkeyname = keyvalues['TimeZoneKeyName'].split('\x00', 1)[0] else: # Windows 2000 or XP # This is the localized name: tzwin = keyvalues['StandardName'] # Open the list of timezones to look up the real name: TZKEYNAME = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones' tzkey = winreg.OpenKey(handle, TZKEYNAME) # Now, match this value to Time Zone information tzkeyname = None for i in range(winreg.QueryInfoKey(tzkey)[0]): subkey = winreg.EnumKey(tzkey, i) sub = winreg.OpenKey(tzkey, subkey) data = valuestodict(sub) sub.Close() if data['Std'] == tzwin: tzkeyname = subkey break tzkey.Close() handle.Close() if tzkeyname is None: raise LookupError('Can not find Windows timezone configuration') timezone = tz_names.get(tzkeyname) if timezone is None: # Nope, that didn't work. Try adding 'Standard Time', # it seems to work a lot of times: timezone = tz_names.get(tzkeyname + ' Standard Time') # Return what we have. if timezone is None: raise pytz.UnknownTimeZoneError('Can not find timezone ' + tzkeyname) return timezone
def read_registry(self): """Extract resolver configuration from the Windows registry.""" lm = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE) want_scan = False try: try: # XP, 2000 tcp_params = _winreg.OpenKey(lm, r'SYSTEM\CurrentControlSet' r'\Services\Tcpip\Parameters') want_scan = True except EnvironmentError: # ME tcp_params = _winreg.OpenKey(lm, r'SYSTEM\CurrentControlSet' r'\Services\VxD\MSTCP') try: self._config_win32_fromkey(tcp_params) finally: tcp_params.Close() if want_scan: interfaces = _winreg.OpenKey(lm, r'SYSTEM\CurrentControlSet' r'\Services\Tcpip\Parameters' r'\Interfaces') try: i = 0 while True: try: guid = _winreg.EnumKey(interfaces, i) i += 1 key = _winreg.OpenKey(interfaces, guid) if not self._win32_is_nic_enabled(lm, guid, key): continue try: self._config_win32_fromkey(key) finally: key.Close() except EnvironmentError: break finally: interfaces.Close() finally: lm.Close()