我们从Python开源项目中,提取了以下25个代码示例,用于说明如何使用winreg.CreateKey()。
def register(classobj): import winreg subKeyCLSID = "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s" % classobj._reg_clsid_ try: hKey = winreg.CreateKey( winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) subKey = winreg.SetValueEx( hKey, "ButtonText", 0, winreg.REG_SZ, classobj._button_text_ ) winreg.SetValueEx( hKey, "ClsidExtension", 0, winreg.REG_SZ, classobj._reg_clsid_ ) # reg value for calling COM object winreg.SetValueEx( hKey, "CLSID", 0, winreg.REG_SZ, "{1FBA04EE-3024-11D2-8F1F-0000F87ABD16}" ) # CLSID for button that sends command to COM object winreg.SetValueEx( hKey, "Default Visible", 0, winreg.REG_SZ, "Yes" ) winreg.SetValueEx( hKey, "ToolTip", 0, winreg.REG_SZ, classobj._tool_tip_ ) winreg.SetValueEx( hKey, "Icon", 0, winreg.REG_SZ, classobj._icon_) winreg.SetValueEx( hKey, "HotIcon", 0, winreg.REG_SZ, classobj._hot_icon_) except WindowsError: print("Couldn't set standard toolbar reg keys.") else: print("Set standard toolbar reg keys.")
def unregister(classobj): import winreg subKeyCLSID = "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s" % classobj._reg_clsid_ try: hKey = winreg.CreateKey( winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) subKey = winreg.DeleteValue( hKey, "ButtonText" ) winreg.DeleteValue( hKey, "ClsidExtension" ) # for calling COM object winreg.DeleteValue( hKey, "CLSID" ) winreg.DeleteValue( hKey, "Default Visible" ) winreg.DeleteValue( hKey, "ToolTip" ) winreg.DeleteValue( hKey, "Icon" ) winreg.DeleteValue( hKey, "HotIcon" ) winreg.DeleteKey( winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) except WindowsError: print("Couldn't delete Standard toolbar regkey.") else: print("Deleted Standard toolbar regkey.") # # test implementation #
def config_win(): try: import winreg as reg key = reg.CreateKey(reg.HKEY_CURRENT_USER, 'SOFTWARE\\Classes\\nzblnk') reg.SetValue(key, '', reg.REG_SZ, 'URL:nzblnk') reg.SetValueEx(key, 'URL Protocol', 0, reg.REG_SZ, '') reg.CloseKey(key) key = reg.CreateKey(reg.HKEY_CURRENT_USER, 'SOFTWARE\\Classes\\nzblnk\\shell\\open\\command') reg.SetValue(key, '', reg.REG_SZ, '"{0}" "%1"'.format(op.normpath(os.path.abspath(sys.executable)))) reg.CloseKey(key) except (OSError, ImportError): print(Col.FAIL + ' FAILED to setup registry link for NZBLNK scheme!' + Col.OFF) sleep(wait_time) sys.exit(2)
def DllRegisterServer(): import winreg key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\" \ "Explorer\\Desktop\\Namespace\\" + \ ShellFolderRoot._reg_clsid_) winreg.SetValueEx(key, None, 0, winreg.REG_SZ, ShellFolderRoot._reg_desc_) # And special shell keys under our CLSID key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "CLSID\\" + ShellFolderRoot._reg_clsid_ + "\\ShellFolder") # 'Attributes' is an int stored as a binary! use struct attr = shellcon.SFGAO_FOLDER | shellcon.SFGAO_HASSUBFOLDER | \ shellcon.SFGAO_BROWSABLE import struct s = struct.pack("i", attr) winreg.SetValueEx(key, "Attributes", 0, winreg.REG_BINARY, s) print(ShellFolderRoot._reg_desc_, "registration complete.")
def EnableCrashDumpCollection(): """Tell Windows Error Reporting to record crash dumps so that we can diagnose linker crashes and other toolchain failures. Documented at: https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181.aspx """ if sys.platform == 'win32' and os.environ.get('CHROME_HEADLESS') == '1': key_name = r'SOFTWARE\Microsoft\Windows\Windows Error Reporting' try: key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, key_name) # Merely creating LocalDumps is sufficient to enable the defaults. winreg.CreateKey(key, "LocalDumps") # Disable the WER UI, as documented here: # https://msdn.microsoft.com/en-us/library/windows/desktop/bb513638.aspx winreg.SetValueEx(key, "DontShowUI", 0, winreg.REG_DWORD, 1) # Trap OSError instead of WindowsError so pylint will succeed on Linux. # Catching errors is important because some build machines are not elevated # and writing to HKLM requires elevation. except OSError: pass
def EnableCrashDumpCollection(): """Tell Windows Error Reporting to record crash dumps so that we can diagnose linker crashes and other toolchain failures. Documented at: https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181.aspx """ if sys.platform == 'win32' and os.environ.get('CHROME_HEADLESS') == '1': key_name = r'SOFTWARE\Microsoft\Windows\Windows Error Reporting' try: key = winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, key_name, 0, winreg.KEY_WOW64_64KEY | winreg.KEY_ALL_ACCESS) # Merely creating LocalDumps is sufficient to enable the defaults. winreg.CreateKey(key, "LocalDumps") # Disable the WER UI, as documented here: # https://msdn.microsoft.com/en-us/library/windows/desktop/bb513638.aspx winreg.SetValueEx(key, "DontShowUI", 0, winreg.REG_DWORD, 1) # Trap OSError instead of WindowsError so pylint will succeed on Linux. # Catching errors is important because some build machines are not elevated # and writing to HKLM requires elevation. except OSError: pass
def setValueFromRegKey(self, key, valueName, valueData, valueType): """Sets a value in a key @param key: The registry key that holds the value to set. If the key does not exist, it will be created. 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 set @type valueName: string @param valueData: The data to assign to the value @type valueData: string @param valueType: The type of the value @type valueType: WinRegValueType """ hkey, key = self._getHiveAndKey(key) aReg = reg.ConnectRegistry(None, hkey) aKey = reg.CreateKey(aReg, key) reg.SetValueEx(aKey, valueName, 0, valueType.type, valueData)
def addActionToShell(self, name, descr, cmd): """ add action in windows explorer on top of file & dir """ if descr == "": descr = name import winreg as winreg for item in ["*", "Directory"]: key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, r'%s\shell\%s' % (item, name)) key2 = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, r'%s\shell\%s\Command' % (item, name)) winreg.SetValueEx(key, "", None, winreg.REG_SZ, "%s " % descr) winreg.SetValueEx(key, "Icon", None, winreg.REG_SZ, "") winreg.SetValueEx(key, "Position", None, winreg.REG_SZ, "Top") winreg.SetValueEx(key, "", None, winreg.REG_SZ, "%s " % descr) #winreg.SetValueEx(key2,"",None,winreg.REG_SZ,r'cmd.exe /s /k pushd "%V"') winreg.SetValueEx(key2, "", None, winreg.REG_SZ, cmd) winreg.CloseKey(key)
def get_installed_pythons(): try: python_core = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, "Software\\Python\\PythonCore") except WindowsError: # No registered Python installations return {} i = 0 versions = [] while True: try: versions.append(winreg.EnumKey(python_core, i)) i = i + 1 except WindowsError: break exes = dict() for ver in versions: try: path = winreg.QueryValue(python_core, "%s\\InstallPath" % ver) except WindowsError: continue exes[ver] = join(path, "python.exe") winreg.CloseKey(python_core) # Add the major versions # Sort the keys, then repeatedly update the major version entry # Last executable (i.e., highest version) wins with this approach for ver in sorted(exes): exes[ver[0]] = exes[ver] return exes
def DllRegisterServer(): # Also need to register specially in: # HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches # See link at top of file. import winreg kn = r"Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%s" \ % (EmptyVolumeCache._reg_desc_,) key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, kn) winreg.SetValueEx(key, None, 0, winreg.REG_SZ, EmptyVolumeCache._reg_clsid_)
def DllRegisterServer(): import winreg if sys.getwindowsversion()[0] < 6: print("This sample only works on Vista") sys.exit(1) key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\" \ "Explorer\\Desktop\\Namespace\\" + \ ShellFolder._reg_clsid_) winreg.SetValueEx(key, None, 0, winreg.REG_SZ, ShellFolder._reg_desc_) # And special shell keys under our CLSID key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "CLSID\\" + ShellFolder._reg_clsid_ + "\\ShellFolder") # 'Attributes' is an int stored as a binary! use struct attr = shellcon.SFGAO_FOLDER | shellcon.SFGAO_HASSUBFOLDER | \ shellcon.SFGAO_BROWSABLE import struct s = struct.pack("i", attr) winreg.SetValueEx(key, "Attributes", 0, winreg.REG_BINARY, s) # register the context menu handler under the FolderViewSampleType type. keypath = "%s\\shellex\\ContextMenuHandlers\\%s" % (ContextMenu._context_menu_type_, ContextMenu._reg_desc_) key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, keypath) winreg.SetValueEx(key, None, 0, winreg.REG_SZ, ContextMenu._reg_clsid_) propsys.PSRegisterPropertySchema(get_schema_fname()) print(ShellFolder._reg_desc_, "registration complete.")
def DllRegisterServer(): import winreg key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "Python.File\\shellex") subkey = winreg.CreateKey(key, "IconHandler") winreg.SetValueEx(subkey, None, 0, winreg.REG_SZ, ShellExtension._reg_clsid_) print(ShellExtension._reg_desc_, "registration complete.")
def DllRegisterServer(): import winreg key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "directory\\shellex\\CopyHookHandlers\\" + ShellExtension._reg_desc_) winreg.SetValueEx(key, None, 0, winreg.REG_SZ, ShellExtension._reg_clsid_) key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "*\\shellex\\CopyHookHandlers\\" + ShellExtension._reg_desc_) winreg.SetValueEx(key, None, 0, winreg.REG_SZ, ShellExtension._reg_clsid_) print(ShellExtension._reg_desc_, "registration complete.")
def DllRegisterServer(): import winreg key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, "Python.File\\shellex") subkey = winreg.CreateKey(key, "ContextMenuHandlers") subkey2 = winreg.CreateKey(subkey, "PythonSample") winreg.SetValueEx(subkey2, None, 0, winreg.REG_SZ, ShellExtension._reg_clsid_) print(ShellExtension._reg_desc_, "registration complete.")
def _doregister(mod_name, dll_name): assert os.path.isfile(dll_name), "Shouldn't get here if the file doesn't exist!" try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "Software\\Python\\PythonCore\\%s\\Modules\\%s" % (sys.winver, mod_name)) except winreg.error: try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "Software\\Python\\PythonCore\\%s\\Modules\\%s" % (sys.winver, mod_name)) except winreg.error: print("Could not find the existing '%s' module registered in the registry" % (mod_name,)) usage_and_die(4) # Create the debug key. sub_key = winreg.CreateKey(key, "Debug") winreg.SetValue(sub_key, None, winreg.REG_SZ, dll_name) print("Registered '%s' in the registry" % (dll_name,))
def RegisterAddin(klass): import winreg key = winreg.CreateKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Excel\\Addins") subkey = winreg.CreateKey(key, klass._reg_progid_) winreg.SetValueEx(subkey, "CommandLineSafe", 0, winreg.REG_DWORD, 0) winreg.SetValueEx(subkey, "LoadBehavior", 0, winreg.REG_DWORD, 3) winreg.SetValueEx(subkey, "Description", 0, winreg.REG_SZ, "Excel Addin") winreg.SetValueEx(subkey, "FriendlyName", 0, winreg.REG_SZ, "A Simple Excel Addin")
def DllRegisterServer(): comclass = IEToolbar # register toolbar with IE try: print("Trying to register Toolbar.\n") hkey = winreg.CreateKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Internet Explorer\\Toolbar" ) subKey = winreg.SetValueEx( hkey, comclass._reg_clsid_, 0, winreg.REG_BINARY, "\0" ) except WindowsError: print("Couldn't set registry value.\nhkey: %d\tCLSID: %s\n" % ( hkey, comclass._reg_clsid_ )) else: print("Set registry value.\nhkey: %d\tCLSID: %s\n" % ( hkey, comclass._reg_clsid_ )) # TODO: implement reg settings for standard toolbar button # unregister plugin
def DllUnregisterServer(): comclass = IEToolbar # unregister toolbar from internet explorer try: print("Trying to unregister Toolbar.\n") hkey = winreg.CreateKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Internet Explorer\\Toolbar" ) winreg.DeleteValue( hkey, comclass._reg_clsid_ ) except WindowsError: print("Couldn't delete registry value.\nhkey: %d\tCLSID: %s\n" % ( hkey, comclass._reg_clsid_ )) else: print("Deleting reg key succeeded.\n") # entry point
def set_password(self, service, username, password): """Write the password to the registry """ # encrypt the password password_encrypted = _win_crypto.encrypt(password.encode('utf-8')) # encode with base64 password_base64 = base64.encodestring(password_encrypted) # encode again to unicode password_saved = password_base64.decode('ascii') # store the password key_name = r'Software\%s\Keyring' % service hkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER, key_name) winreg.SetValueEx(hkey, username, 0, winreg.REG_SZ, password_saved)
def jit(vdb, line): ''' Enable/Disable the current VDB location as the current Just-In-Time debugger for windows applications. Usage: jitenable [-D] -E Enable VDB JIT debugging -D Disable JIT debugging ''' argv = e_cli.splitargs(line) try: opts, args = getopt.getopt(argv, "ED") except Exception as e: return vdb.do_help('jit') try: import winreg except Exception as e: vdb.vprint('Error Importing _winreg: %s' % e) return HKLM = winreg.HKEY_LOCAL_MACHINE HKCU = winreg.HKEY_CURRENT_USER REG_SZ = winreg.REG_SZ regpath = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug' # wow64path = r'SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug' # regkey = _winreg.CreateKey(HKLM, regpath) regkey = winreg.CreateKey(HKLM, regpath) vdb.vprint('JIT Currently: %s' % winreg.QueryValueEx(regkey, 'Debugger')[0]) setval = None for opt, optarg in opts: if opt == '-D': setval = '' elif opt == '-E': vdbpath = os.path.abspath(sys.argv[0]) setval = '%s %s -r -p %%ld -e %%Id' % (sys.executable, vdbpath) # _winreg.SetValue(HKLM if setval != None: vdb.vprint('Setting JIT: %s' % (setval,)) winreg.SetValueEx(regkey, 'Debugger', None, REG_SZ, setval)
def set_subkey(self,name,value=None): """Create the named subkey and set its value. There are several different ways to specify the new contents of the named subkey: * if 'value' is the Key class, a subclass thereof, or None, then the subkey is created but not populated with any data. * if 'value' is a key instance, the data from that key will be copied into the new subkey. * if 'value' is a dictionary, the dict's keys are interpreted as key or value names and the corresponding entries are created within the new subkey - nested dicts create further subkeys, while scalar values create values on the subkey. * any other value will be converted to a Value object and assigned to the default value for the new subkey. """ self.sam |= KEY_CREATE_SUB_KEY subkey = Key(name,self) try: subkey = self.get_subkey(name) except AttributeError: _winreg.CreateKey(self.hkey,name) subkey = self.get_subkey(name) if value is None: pass elif issubclass(type(value),type) and issubclass(value,Key): pass elif isinstance(value,Key): for v in value.values(): subkey[v.name] = v for k in value.subkeys(): subkey.set_subkey(k.name,k) elif isinstance(value,dict): for (nm,val) in value.items(): if isinstance(val,dict): subkey.set_subkey(nm,val) elif isinstance(val,Key): subkey.set_subkey(nm,val) elif issubclass(type(val),type) and issubclass(val,Key): subkey.set_subkey(nm,val) else: subkey[nm] = val else: if not isinstance(value,Value): value = Value(value) subkey[value.name] = value