我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用_winreg.SetValueEx()。
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 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 enable(self): ''' @summary: Disables Windows Task Manager ''' key_exists = False # Try to read the key try: reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION) disabled = _winreg.QueryValueEx(reg, "DisableTaskMgr")[0] _winreg.CloseKey(reg) key_exists = True except: pass # If key exists and is disabled, enable it if key_exists and disabled: reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION, 0, _winreg.KEY_SET_VALUE) _winreg.SetValueEx(reg, "DisableTaskMgr", 0, _winreg.REG_DWORD, 0x00000000) _winreg.CloseKey(reg)
def modify(): pythonpath = os.path.dirname(os.path.normpath(sys.executable)) scripts = os.path.join(pythonpath, "Scripts") appdata = os.environ["APPDATA"] if hasattr(site, "USER_SITE"): userpath = site.USER_SITE.replace(appdata, "%APPDATA%") userscripts = os.path.join(userpath, "Scripts") else: userscripts = None with _winreg.CreateKey(HKCU, ENV) as key: try: envpath = _winreg.QueryValueEx(key, PATH)[0] except WindowsError: envpath = DEFAULT paths = [envpath] for path in (pythonpath, scripts, userscripts): if path and path not in envpath and os.path.isdir(path): paths.append(path) envpath = os.pathsep.join(paths) _winreg.SetValueEx(key, PATH, 0, _winreg.REG_EXPAND_SZ, envpath) return paths, envpath
def save(self): if USE_WINDOWS: import _winreg try: key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, self.keyname, sam=_winreg.KEY_SET_VALUE | _winreg.KEY_WRITE) except: key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, self.keyname) try: for k, v in self.values.iteritems(): _winreg.SetValueEx(key, str(k), 0, _winreg.REG_SZ, str(v)) finally: key.Close() else: d = os.path.dirname(self.filename) if not os.path.isdir(d): os.makedirs(d) f = open(self.filename, 'w') try: data = '\n'.join(["%s=%s" % (k,v) for k,v in self.values.iteritems()]) f.write(data) finally: f.close()
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 __setitem__(self, item, value): item = str(item) pyvalue = type(value) if pyvalue is tuple and len(value)==2: valuetype = value[1] value = value[0] else: if pyvalue is dict or isinstance(value, RegistryDict): d = RegistryDict(self.keyhandle, item) d.clear() d.update(value) return if pyvalue is str: valuetype = _winreg.REG_SZ elif pyvalue is int: valuetype = _winreg.REG_DWORD else: valuetype = _winreg.REG_BINARY value = 'PyPickle' + cPickle.dumps(value) _winreg.SetValueEx(self.keyhandle, item, 0, valuetype, value)
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 onUninstall(): path = os.path.join(config.getUserDefaultConfigPath(), ".dbInstall") if os.path.exists(path): #This is an update. Bail. os.remove(path) return key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, "Environment", 0, _winreg.KEY_READ | _winreg.KEY_WRITE) try: value, typ = _winreg.QueryValueEx(key, "Path") except: return if value is None or value == "": return dir = os.path.dirname(__file__) if not isinstance(dir, unicode): dir = dir.decode(sys.getfilesystemencoding()) dir = dir.replace(addonHandler.DELETEDIR_SUFFIX, "") if value.find(dir) != -1: value = value.replace(";" + dir, "") value = value.replace(dir + ";", "") value = value.replace(dir, "") _winreg.SetValueEx(key, "Path", None, typ, value) sendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
def __setitem__(self, key, value): 'Assign the item to a value.' if isinstance(value, self.TYPES): _winreg.SetValueEx(self.__self, key, 0, list(self.TYPES).index(value.__class__), value.value) else: _winreg.SetValueEx(self.__self, key, 0, _winreg.QueryValueEx(self.__self, key)[1], value)
def setVal(self, name, val, typ='str'): # set value typ=typ.upper() if typ=='DW': typ='DWL' typ=rTyp[typ] reg.SetValueEx(self.wrk, name, 0, typ, val)
def __setitem__(self, key, value): 'Assign the item to a value.' if isinstance(value, self.TYPES): _winreg.SetValueEx(self.__key, key, 0, list(self.TYPES).index(value.__class__), value.value) else: _winreg.SetValueEx(self.__key, key, 0, _winreg.QueryValueEx(self.__key, key)[1], value)
def regSetDword(keyName, val): reg.SetValueEx(RegisterKey, keyName, 0, reg.REG_DWORD, val)
def regSetString(keyName, val): reg.SetValueEx(RegisterKey, keyName, 0, reg.REG_SZ, val)
def set(self, name, value, type=None): """Set the value for ``name`` to ``value``. if ``type`` is None try to guess items""" if type is None: type = self._guess_value_type(value) if type == REG_QWORD: value = struct.pack("<Q", value) return _winreg.SetValueEx(self.phkey, name, 0, type, value)
def windows_persistence(): import _winreg from _winreg import HKEY_CURRENT_USER as HKCU run_key = r'Software\Microsoft\Windows\CurrentVersion\Run' bin_path = sys.executable try: reg_key = _winreg.OpenKey(HKCU, run_key, 0, _winreg.KEY_WRITE) _winreg.SetValueEx(reg_key, 'br', 0, _winreg.REG_SZ, bin_path) _winreg.CloseKey(reg_key) return True, 'HKCU Run registry key applied' except WindowsError: return False, 'HKCU Run registry key failed'
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 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 RegisterAddin(klass): import _winreg key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\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, klass._reg_progid_) _winreg.SetValueEx(subkey, "FriendlyName", 0, _winreg.REG_SZ, klass._reg_progid_)
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 create_value(self, key, value_name, value): """ Creates a value THAT DOESN'T EXIST, we need to keep track of the keys that we are creating """ self.no_restore = False try: return winreg.SetValueEx(key, value_name, 0, winreg.REG_SZ, value) except WindowsError as error: print "Error al crear clave" self.no_restore = True
def add(name, application): """add a new autostart entry""" key = get_runonce() _winreg.SetValueEx(key, name, 0, _winreg.REG_SZ, application) _winreg.CloseKey(key)
def setenv(self, name, value): # Note: for 'system' scope, you must run this as Administrator key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_ALL_ACCESS) winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value) winreg.CloseKey(key) # For some strange reason, calling SendMessage from the current process # doesn't propagate environment changes at all. # TODO: handle CalledProcessError (for assert) check_call('''\ "%s" -c "import win32api, win32con; assert win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')"''' % sys.executable)
def disable(self): ''' @summary: Disables Windows Task Manager ''' key_exists = False # Try to read the key try: reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION) disabled = _winreg.QueryValueEx(reg, "DisableTaskMgr")[0] _winreg.CloseKey(reg) key_exists = True except: pass # If key doesn't exist, create it and set to disabled if not key_exists: reg = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION) _winreg.SetValueEx(reg, "DisableTaskMgr", 0, _winreg.REG_DWORD, 0x00000001) _winreg.CloseKey(reg) # If enabled, disable it elif key_exists and not disabled: reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, self.DISABLE_KEY_LOCATION, 0, _winreg.KEY_SET_VALUE) _winreg.SetValueEx(reg, "DisableTaskMgr", 0, _winreg.REG_DWORD, 0x00000001) _winreg.CloseKey(reg)
def __add_to_startup_programs(self): ''' @summary: Adds Crypter to the list of Windows startup programs @todo: Code and test @todo: Restore try and except catch ''' try: reg = _winreg.CreateKeyEx(_winreg.HKEY_CURRENT_USER, self.STARTUP_REGISTRY_LOCATION) _winreg.SetValueEx(reg, "Crypter", 0, _winreg.REG_SZ, sys.executable) _winreg.CloseKey(reg) except WindowsError: pass
def SetPyKeyVal(key_name, value_name, value): root_hkey = get_root_hkey() root_key = winreg.OpenKey(root_hkey, root_key_name) try: my_key = winreg.CreateKey(root_key, key_name) try: winreg.SetValueEx(my_key, value_name, 0, winreg.REG_SZ, value) finally: my_key.Close() finally: root_key.Close() if verbose: print "-> %s\\%s[%s]=%r" % (root_key_name, key_name, value_name, value)