我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用_winreg.OpenKey()。
def windefnd_running(): key = False if reg_exists('SOFTWARE\\Policies\\Microsoft\\Windows Defender'): key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SOFTWARE\\Policies\\Microsoft\\Windows Defender') elif reg_exists('SOFTWARE\\Microsoft\\Windows Defender'): key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SOFTWARE\\Microsoft\\Windows Defender') if key: try: val=_winreg.QueryValueEx(key, "DisableAntiSpyware") if val[0] == 1: return False else: return True except: return False print "BROKEEEEEEN"
def _get_win_folder_from_registry(csidl_name): """This is a fallback technique at best. I'm not sure if using the registry for this guarantees us the correct answer for all CSIDL_* names. """ import _winreg shell_folder_name = { "CSIDL_APPDATA": "AppData", "CSIDL_COMMON_APPDATA": "Common AppData", "CSIDL_LOCAL_APPDATA": "Local AppData", }[csidl_name] key = _winreg.OpenKey( _winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" ) dir, type = _winreg.QueryValueEx(key, shell_folder_name) return dir
def _get_win_folder_from_registry(csidl_name): """ This is a fallback technique at best. I'm not sure if using the registry for this guarantees us the correct answer for all CSIDL_* names. """ import _winreg shell_folder_name = { "CSIDL_APPDATA": "AppData", "CSIDL_COMMON_APPDATA": "Common AppData", "CSIDL_LOCAL_APPDATA": "Local AppData", }[csidl_name] key = _winreg.OpenKey( _winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" ) directory, _type = _winreg.QueryValueEx(key, shell_folder_name) return directory
def _get_win_folder_from_registry(csidl_name): """This is a fallback technique at best. I'm not sure if using the registry for this guarantees us the correct answer for all CSIDL_* names. """ if PY3: import winreg as _winreg else: import _winreg shell_folder_name = { "CSIDL_APPDATA": "AppData", "CSIDL_COMMON_APPDATA": "Common AppData", "CSIDL_LOCAL_APPDATA": "Local AppData", }[csidl_name] key = _winreg.OpenKey( _winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" ) dir, type = _winreg.QueryValueEx(key, shell_folder_name) return dir
def open(self, key, subkey): """ Opens a Windows registry key. The first param is one of the Windows HKEY names or an already open key. The second param is the actual key to open. """ if type(key) is str: hkey = Registry.map_key(key) else: hkey = key if not hkey: raise RegistryError,"could not find registry key for %s" % key try: self._hkey = wreg.OpenKey(hkey, subkey) self._keyname = subkey except EnvironmentError, e: raise RegistryError, e
def _get_reg_value(key, subkey, name): """Return registry value specified by key, subkey, and name. Environment variables in values of type REG_EXPAND_SZ are expanded if possible. """ key = _winreg.OpenKey(key, subkey) try: ret = _winreg.QueryValueEx(key, name) except WindowsError: return None else: key.Close() if ret[1] == _winreg.REG_EXPAND_SZ: return expandvars(ret[0]) else: return ret[0]
def get_shellfolders(branch=HKCU, key=SHELL_FOLDERS): """Return mapping of shell folder names (current user) to paths.""" key = _winreg.OpenKey(branch, key) folders = {} i = 0 while True: try: ret = _winreg.EnumValue(key, i) if ret[2] == _winreg.REG_EXPAND_SZ: folders[ret[0]] = expandvars(ret[1]) else: folders[ret[0]] = ret[1] except WindowsError: break i +=1 key.Close() return folders
def test_self_iat_hook_success(): """Test hook success in single(self) thread""" pythondll_mod = [m for m in windows.current_process.peb.modules if m.name.startswith("python") and m.name.endswith(".dll")][0] RegOpenKeyExA = [n for n in pythondll_mod.pe.imports['advapi32.dll'] if n.name == "RegOpenKeyExA"][0] hook_value = [] @windows.hooks.RegOpenKeyExACallback def open_reg_hook(hKey, lpSubKey, ulOptions, samDesired, phkResult, real_function): hook_value.append((hKey, lpSubKey.value)) phkResult[0] = 12345678 return 0 x = RegOpenKeyExA.set_hook(open_reg_hook) import _winreg open_args = (0x12345678, "MY_KEY_VALUE") k = _winreg.OpenKey(*open_args) assert k.handle == 12345678 assert hook_value[0] == open_args # Remove the hook x.disable()
def test_self_iat_hook_fail_return(): """Test hook fail in single(self) thread""" pythondll_mod = [m for m in windows.current_process.peb.modules if m.name.startswith("python") and m.name.endswith(".dll")][0] RegOpenKeyExA = [n for n in pythondll_mod.pe.imports['advapi32.dll'] if n.name == "RegOpenKeyExA"][0] @windows.hooks.RegOpenKeyExACallback def open_reg_hook_fail(hKey, lpSubKey, ulOptions, samDesired, phkResult, real_function): return 0x11223344 x = RegOpenKeyExA.set_hook(open_reg_hook_fail) import _winreg open_args = (0x12345678, "MY_KEY_VALUE") with pytest.raises(WindowsError) as ar: _winreg.OpenKey(*open_args) assert ar.value.winerror == 0x11223344 x.disable()
def selfdestruct(plat): if plat == 'win': import _winreg from _winreg import HKEY_CURRENT_USER as HKCU run_key = r'Software\Microsoft\Windows\CurrentVersion\Run' try: reg_key = _winreg.OpenKey(HKCU, run_key, 0, _winreg.KEY_ALL_ACCESS) _winreg.DeleteValue(reg_key, 'br') _winreg.CloseKey(reg_key) except WindowsError: pass elif plat == 'nix': pass elif plat == 'mac': pass # self delete os.remove(sys.argv[0]) sys.exit(0)
def selfdestruct(plat): if plat == 'win': import _winreg from _winreg import HKEY_CURRENT_USER as HKCU run_key = r'Software\Microsoft\Windows\CurrentVersion\Run' try: reg_key = _winreg.OpenKey(HKCU, run_key, 0, _winreg.KEY_ALL_ACCESS) _winreg.DeleteValue(reg_key, 'br') _winreg.CloseKey(reg_key) except WindowsError: pass elif plat == 'nix': pass elif plat == 'mac': pass os.remove(sys.argv[0]) sys.exit(0)
def _get_win_folder_from_registry(csidl_name): """This is a fallback technique at best. I'm not sure if using the registry for this guarantees us the correct answer for all CSIDL_* names. """ import _winreg shell_folder_name = { "CSIDL_APPDATA": "AppData", "CSIDL_COMMON_APPDATA": "Common AppData", "CSIDL_LOCAL_APPDATA": "Local AppData", }[csidl_name] key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") dir, type = _winreg.QueryValueEx(key, shell_folder_name) return dir
def _RegistryGetValueUsingWinReg(key, value): """Use the _winreg module to obtain the value of a registry key. Args: key: The registry key. value: The particular registry value to read. Return: contents of the registry key's value, or None on failure. Throws ImportError if _winreg is unavailable. """ import _winreg try: root, subkey = key.split('\\', 1) assert root == 'HKLM' # Only need HKLM for now. with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey: return _winreg.QueryValueEx(hkey, value)[0] except WindowsError: return None