Python winreg.KEY_READ Examples

The following are 30 code examples of winreg.KEY_READ(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module winreg , or try the search function .
Example #1
Source File: _core.py    From photoshop-python-api with MIT License 7 votes vote down vote up
def open_key(key):
        """Open the register key.

        Args:
            key (str): The key of register.

        Returns:
            str: The handle to the specified key.

        """
        machine_type = platform.machine()
        mappings = {"AMD64": winreg.KEY_WOW64_64KEY}
        return winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE,
            key,
            access=winreg.KEY_READ | mappings.get(machine_type,
                                                  winreg.KEY_WOW64_32KEY),
        ) 
Example #2
Source File: uninstall.py    From pyxll-examples with The Unlicense 6 votes vote down vote up
def uninstall_all():
    """uninstalls PyXLL from all installed Excel versions"""
    for wow64_flags in (winreg.KEY_WOW64_64KEY, winreg.KEY_WOW64_32KEY):
        for root in _root_keys.keys():
            try:
                flags = wow64_flags | winreg.KEY_READ
                office_root = winreg.OpenKey(root, r"Software\Microsoft\Office", 0, flags)
            except WindowsError:
                continue

            # look for all installed versions of Excel and uninstall PyXLL
            i = 0
            while True:
                try:
                    subkey = winreg.EnumKey(office_root, i)
                except WindowsError:
                    break

                match = re.match("^(\d+(?:\.\d+)?)$", subkey)
                if match:
                    office_version = match.group(1)
                    uninstall(office_root, office_version, wow64_flags)
                i += 1

            winreg.CloseKey(office_root) 
Example #3
Source File: mapiex.py    From nsf2x with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #4
Source File: __init__.py    From SendCode with MIT License 6 votes vote down vote up
def conemu_setup():
    global CONEMU_SETUP

    if CONEMU_SETUP:
        return

    try:
        akey = winreg.OpenKey(
            winreg.HKEY_CLASSES_ROOT, "Directory\\shell\\ConEmu Here\\command", 0, winreg.KEY_READ)
        command = winreg.QueryValueEx(akey, "")[0]
        conemu_base_dir = os.path.join(RE_CONEMU.match(command).group(1), "ConEmu")
        if os.path.exists(conemu_base_dir):
            if conemu_base_dir not in os.environ["PATH"]:
                os.environ["PATH"] = conemu_base_dir + ";" + os.environ["PATH"]
                CONEMU_SETUP = True
    except Exception:
        return 
Example #5
Source File: get_applications.py    From BoomER with GNU General Public License v3.0 6 votes vote down vote up
def get_apps_values(self, h_key, key_path):
        key = winreg.OpenKey(h_key, key_path, 0, winreg.KEY_READ)
        i = 0
        name = ""
        version = "???"
        while True:
            try:
                subkey = winreg.EnumValue(key, i)
                result = subkey[0]
                if result == "DisplayName":
                    name = subkey[1]
                elif result == "DisplayVersion":
                    version = subkey[1]
                i+=1
            except WindowsError as e:
                break
        if name != "":
            data = name + " ---> " + version
            print(data)
            if self.file_open:
                self.file_open.write(data + "\n")
        winreg.CloseKey(key) 
Example #6
Source File: __init__.py    From SendCode with MIT License 6 votes vote down vote up
def cmder_setup():
    global CMDER_SETUP

    if CMDER_SETUP:
        return

    try:
        akey = winreg.OpenKey(
            winreg.HKEY_CLASSES_ROOT, "Directory\\shell\\Cmder\\command", 0, winreg.KEY_READ)
        command = winreg.QueryValueEx(akey, "")[0]
        conemu_base_dir = os.path.join(
            RE_CMDER.match(command).group(1), "vendor", "conemu-maximus5", "ConEmu")
        if os.path.exists(conemu_base_dir):
            if conemu_base_dir not in os.environ["PATH"]:
                os.environ["PATH"] = conemu_base_dir + ";" + os.environ["PATH"]
                CMDER_SETUP = True
    except Exception:
        return 
Example #7
Source File: _registry.py    From pipenv with MIT License 6 votes vote down vote up
def get_all_values(self):
        schema = {}
        for subkey in self:
            schema[subkey.name] = subkey.get_all_values()

        key = winreg.OpenKeyEx(self._root, self.subkey, 0, winreg.KEY_READ | self._flags)
        try:
            with key:
                for i in count():
                    vname, value, vtype = winreg.EnumValue(key, i)
                    value = get_value_from_tuple(value, vtype)
                    if value:
                        schema[vname or ''] = value
        except OSError:
            pass

        return PythonWrappedDict(schema) 
Example #8
Source File: _registry.py    From pythonfinder with MIT License 6 votes vote down vote up
def get_all_values(self):
        schema = {}
        for subkey in self:
            schema[subkey.name] = subkey.get_all_values()

        key = winreg.OpenKeyEx(self._root, self.subkey, 0, winreg.KEY_READ | self._flags)
        try:
            with key:
                for i in count():
                    vname, value, vtype = winreg.EnumValue(key, i)
                    value = get_value_from_tuple(value, vtype)
                    if value:
                        schema[vname or ''] = value
        except OSError:
            pass

        return PythonWrappedDict(schema) 
Example #9
Source File: reg_watcher.py    From galaxy-integration-humblebundle with GNU General Public License v3.0 6 votes vote down vote up
def _iterate_new_uninstall_keys(self):
        for arch_key in self._ARCH_KEYS:
            for hive in self._LOOKUP_REGISTRY_HIVES:
                with winreg.OpenKey(hive, self._UNINSTALL_LOCATION, 0, winreg.KEY_READ | arch_key) as key:
                    subkeys = winreg.QueryInfoKey(key)[0]

                    cached_subkeys = self.__keys_count[hive | arch_key]
                    self.__keys_count[hive | arch_key] = subkeys
                    if subkeys <= cached_subkeys:
                        continue  # same or lower number of installed programs since last refresh
                    logging.info(f'New keys in registry {hive | arch_key}: {subkeys}. Reparsing.')

                    for i in range(subkeys):
                        subkey_name = winreg.EnumKey(key, i)
                        if self._ignore_filter and self._ignore_filter(subkey_name):
                            logging.debug(f'Filtred out subkey: {subkey_name}')
                            continue
                        with winreg.OpenKey(key, subkey_name) as subkey:
                            yield (subkey_name, subkey) 
Example #10
Source File: edge.py    From NSC_BUILDER with MIT License 6 votes vote down vote up
def _find_edge_win():
    import winreg as reg
    reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe'

    for install_type in reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE:
        try:
            reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
            edge_path = reg.QueryValue(reg_key, None)
            reg_key.Close()
            if not os.path.isfile(edge_path):
                continue
        except WindowsError:
            edge_path = None
        else:
            break

    return edge_path 
Example #11
Source File: chrome.py    From NSC_BUILDER with MIT License 6 votes vote down vote up
def _find_chrome_win():
    import winreg as reg
    reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'

    for install_type in reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE:
        try:
            reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
            chrome_path = reg.QueryValue(reg_key, None)
            reg_key.Close()
            if not os.path.isfile(chrome_path):
                continue
        except WindowsError:
            chrome_path = None
        else:
            break

    return chrome_path 
Example #12
Source File: _msvccompiler.py    From android_universal with MIT License 5 votes vote down vote up
def _find_vc2015():
    try:
        key = winreg.OpenKeyEx(
            winreg.HKEY_LOCAL_MACHINE,
            r"Software\Microsoft\VisualStudio\SxS\VC7",
            access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY
        )
    except OSError:
        log.debug("Visual C++ is not registered")
        return None, None

    best_version = 0
    best_dir = None
    with key:
        for i in count():
            try:
                v, vc_dir, vt = winreg.EnumValue(key, i)
            except OSError:
                break
            if v and vt == winreg.REG_SZ and os.path.isdir(vc_dir):
                try:
                    version = int(float(v))
                except (ValueError, TypeError):
                    continue
                if version >= 14 and version > best_version:
                    best_version, best_dir = version, vc_dir
    return best_version, best_dir 
Example #13
Source File: _winconsole.py    From vistir with ISC License 5 votes vote down vote up
def _get_sid_from_registry():
    try:
        import winreg
    except ImportError:
        import _winreg as winreg
    var_names = ("%USERPROFILE%", "%HOME%")
    current_user_home = next(iter(os.path.expandvars(v) for v in var_names if v), None)
    root, subkey = (
        winreg.HKEY_LOCAL_MACHINE,
        r"Software\Microsoft\Windows NT\CurrentVersion\ProfileList",
    )
    subkey_names = []
    value = None
    matching_key = None
    try:
        with winreg.OpenKeyEx(root, subkey, 0, winreg.KEY_READ) as key:
            for i in count():
                key_name = winreg.EnumKey(key, i)
                subkey_names.append(key_name)
                value = query_registry_value(
                    root, r"{0}\{1}".format(subkey, key_name), "ProfileImagePath"
                )
                if value and value.lower() == current_user_home.lower():
                    matching_key = key_name
                    break
    except OSError:
        pass
    if matching_key is not None:
        return matching_key 
Example #14
Source File: _winconsole.py    From vistir with ISC License 5 votes vote down vote up
def query_registry_value(root, key_name, value):
    try:
        import winreg
    except ImportError:
        import _winreg as winreg
    try:
        with winreg.OpenKeyEx(root, key_name, 0, winreg.KEY_READ) as key:
            return get_value_from_tuple(*winreg.QueryValueEx(key, value))
    except OSError:
        return None 
Example #15
Source File: get_applications.py    From BoomER with GNU General Public License v3.0 5 votes vote down vote up
def search_register(self, option_hk, key_path):
        if option_hk == 0:
            h_key = winreg.HKEY_CURRENT_USER
            data = "HKEY_CURRENT_USER -> "
        else:
            h_key = winreg.HKEY_LOCAL_MACHINE
            data = "HKEY_LOCAL_MACHINE -> "
        self.print_info(data + key_path)
        if self.file_open:
            self.file_open.write("\n" + data + key_path + "\n\n")
        key = winreg.OpenKey(h_key, key_path, 0, winreg.KEY_READ)
        for subkey in self.get_subkeys(key):
            subkey_path = "%s\\%s" % (key_path, subkey)
            self.get_apps_values(h_key, subkey_path)
        winreg.CloseKey(key) 
Example #16
Source File: recipe-577621.py    From code with MIT License 5 votes vote down vote up
def getenv(self, name):
        key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_READ)
        try:
            value, _ = winreg.QueryValueEx(key, name)
        except WindowsError:
            value = ''
        return value 
Example #17
Source File: browsers.py    From tydesk with GNU General Public License v3.0 5 votes vote down vote up
def find_firefox_win():
  import winreg as reg
  reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe'

  for install_type in reg.HKEY_LOCAL_MACHINE, reg.HKEY_CURRENT_USER:
    try:
      reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
      firefox_path = reg.QueryValue(reg_key, None)
      reg_key.Close()
    except WindowsError:
      pass

  return firefox_path 
Example #18
Source File: GXContext.py    From gxpy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_key_based_product_dirs(cls, key='Core', per_user_key=False):
        """
        Gets key product folders based on geosoft.key file and registry

        :param key:              Default Geosoft registry key (in absence of geosoft.key file) to use to discover GX
                                 developer common redistributables or Desktop Applications software (default 'Core')
        :param per_user_key:     Use per-user registry instead of local machine (default False)

        :returns: product_install_dir, user_dir, temp_dir

        .. versionadded:: 9.7
        """
        key_file = os.path.join(os.path.dirname(inspect.getfile(cls)), 'geosoft.key')
        if os.path.exists(key_file):
            with open(key_file) as f:
                key = f.read().strip()
        reg_hive = winreg.HKEY_CURRENT_USER if per_user_key else winreg.HKEY_LOCAL_MACHINE
        env_key = winreg.OpenKey(reg_hive, 'Software\Geosoft\{}\Environment'.format(key), 0,
                                 winreg.KEY_READ)
        try:
            product_install_dir, _ = winreg.QueryValueEx(env_key, 'GEOSOFT')
            user_dir, _ = winreg.QueryValueEx(env_key, 'GEOSOFT2')
            temp_dir, _ = winreg.QueryValueEx(env_key, 'GEOTEMP')
            return product_install_dir, user_dir, temp_dir
        finally:
            winreg.CloseKey(env_key) 
Example #19
Source File: fuse.py    From ff4d with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def Reg32GetValue(rootkey, keyname, valname):
            key, val = None, None
            try:
                key = reg.OpenKey(rootkey, keyname, 0, reg.KEY_READ | reg.KEY_WOW64_32KEY)
                val = str(reg.QueryValueEx(key, valname)[0])
            except WindowsError:
                pass
            finally:
                if key is not None:
                    reg.CloseKey(key)
            return val 
Example #20
Source File: browsers.py    From tydesk with GNU General Public License v3.0 5 votes vote down vote up
def find_chrome_win():
  import winreg as reg
  reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'

  for install_type in reg.HKEY_LOCAL_MACHINE, reg.HKEY_CURRENT_USER:
    try:
      reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
      chrome_path = reg.QueryValue(reg_key, None)
      reg_key.Close()
    except WindowsError:
      pass

  return chrome_path 
Example #21
Source File: QgsFmvInstaller.py    From QGISFMV with GNU General Public License v3.0 5 votes vote down vote up
def WinSoftwareInstalled(hive, flag):
    aReg = winreg.ConnectRegistry(None, hive)
    aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                          0, winreg.KEY_READ | flag)

    count_subkey = winreg.QueryInfoKey(aKey)[0]

    software_list = []

    for i in range(count_subkey):
        software = {}
        try:
            asubkey_name = winreg.EnumKey(aKey, i)
            asubkey = winreg.OpenKey(aKey, asubkey_name)
            software['name'] = winreg.QueryValueEx(asubkey, "DisplayName")[0]

            try:
                software['version'] = winreg.QueryValueEx(asubkey, "DisplayVersion")[0]
            except EnvironmentError:
                software['version'] = 'undefined'
            try:
                software['publisher'] = winreg.QueryValueEx(asubkey, "Publisher")[0]
            except EnvironmentError:
                software['publisher'] = 'undefined'
            software_list.append(software)
        except EnvironmentError:
            continue

    return software_list 
Example #22
Source File: reader.py    From pypykatz with MIT License 5 votes vote down vote up
def find_key(self, key_path, throw = True):
		if self.root is None:
			self.setup()
		if key_path == '' or key_path is None:
			return self.root
			
		key_path = self.hive_name + '\\' + key_path		
		try:
			key = winreg.OpenKeyEx(self.root, key_path, access= winreg.KEY_READ)
		except Exception as e:
			if throw is True:
				raise e
			else:
				return None
		return key 
Example #23
Source File: setup.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def win_get_llvm_reg():
    REG_PATH = "SOFTWARE\\LLVM\\LLVM"
    try:
      return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, REG_PATH, 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
    except FileNotFoundError:
      pass
    return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, REG_PATH, 0, winreg.KEY_READ) 
Example #24
Source File: get_win_browser.py    From PyMicroChat with GNU General Public License v3.0 5 votes vote down vote up
def get_browser_path():
    paths_dict = {}
    for name in BROWSER_PATH:
        handler = winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE, BROWSER_PATH[name], access=winreg.KEY_READ)
        abs_path = winreg.QueryValue(handler, None)
        # print(abs_path)
        paths_dict[name] = abs_path
    return paths_dict 
Example #25
Source File: utils.py    From sublime-ide-r with MIT License 5 votes vote down vote up
def read_registry(key, valueex):
    reg_key = OpenKey(HKEY_LOCAL_MACHINE, key, 0, KEY_READ)
    return QueryValueEx(reg_key, valueex) 
Example #26
Source File: _msvccompiler.py    From setuptools with MIT License 5 votes vote down vote up
def _find_vc2015():
    try:
        key = winreg.OpenKeyEx(
            winreg.HKEY_LOCAL_MACHINE,
            r"Software\Microsoft\VisualStudio\SxS\VC7",
            access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY
        )
    except OSError:
        log.debug("Visual C++ is not registered")
        return None, None

    best_version = 0
    best_dir = None
    with key:
        for i in count():
            try:
                v, vc_dir, vt = winreg.EnumValue(key, i)
            except OSError:
                break
            if v and vt == winreg.REG_SZ and os.path.isdir(vc_dir):
                try:
                    version = int(float(v))
                except (ValueError, TypeError):
                    continue
                if version >= 14 and version > best_version:
                    best_version, best_dir = version, vc_dir
    return best_version, best_dir 
Example #27
Source File: _msvccompiler.py    From Imogen with MIT License 5 votes vote down vote up
def _find_vc2015():
    try:
        key = winreg.OpenKeyEx(
            winreg.HKEY_LOCAL_MACHINE,
            r"Software\Microsoft\VisualStudio\SxS\VC7",
            access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY
        )
    except OSError:
        log.debug("Visual C++ is not registered")
        return None, None

    best_version = 0
    best_dir = None
    with key:
        for i in count():
            try:
                v, vc_dir, vt = winreg.EnumValue(key, i)
            except OSError:
                break
            if v and vt == winreg.REG_SZ and os.path.isdir(vc_dir):
                try:
                    version = int(float(v))
                except (ValueError, TypeError):
                    continue
                if version >= 14 and version > best_version:
                    best_version, best_dir = version, vc_dir
    return best_version, best_dir 
Example #28
Source File: flaskwebgui.py    From flaskwebgui with MIT License 5 votes vote down vote up
def find_chrome_win(self):
        import winreg as reg
        reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'

        for install_type in reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE:
            try:
                reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
                chrome_path = reg.QueryValue(reg_key, None)
                reg_key.Close()
            except WindowsError:
                chrome_path = None
            else:
                break

        return chrome_path 
Example #29
Source File: px.py    From px with MIT License 5 votes vote down vote up
def check_installed():
    ret = True
    runkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
        r"Software\Microsoft\Windows\CurrentVersion\Run", 0, winreg.KEY_READ)
    try:
        winreg.QueryValueEx(runkey, "Px")
    except:
        ret = False
    winreg.CloseKey(runkey)

    return ret 
Example #30
Source File: chrome.py    From Eel with MIT License 5 votes vote down vote up
def _find_chrome_win():
    import winreg as reg
    reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'

    for install_type in reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE:
        try:
            reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
            chrome_path = reg.QueryValue(reg_key, None)
            reg_key.Close()
            if not os.path.isfile(chrome_path):
                continue
        except WindowsError:
            chrome_path = None
        else:
            break

    return chrome_path