Python winreg.CreateKeyEx() Examples

The following are 8 code examples of winreg.CreateKeyEx(). 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: _registry.py    From pythonfinder with MIT License 5 votes vote down vote up
def set_value(self, value_name, value):
        with winreg.CreateKeyEx(self._root, self.subkey, 0, winreg.KEY_WRITE | self._flags) as key:
            if value is None:
                winreg.DeleteValue(key, value_name)
            elif isinstance(value, str):
                winreg.SetValueEx(key, value_name, 0, winreg.REG_SZ, value)
            else:
                raise TypeError('cannot write {} to registry'.format(type(value))) 
Example #2
Source File: _registry.py    From pythonfinder with MIT License 5 votes vote down vote up
def _set_all_values(self, rootkey, name, info, errors):
        with winreg.CreateKeyEx(rootkey, name, 0, winreg.KEY_WRITE | self._flags) as key:
            for k, v in info:
                if isinstance(v, PythonWrappedDict):
                    self._set_all_values(key, k, v._items(), errors)
                elif isinstance(v, dict):
                    self._set_all_values(key, k, v.items(), errors)
                elif v is None:
                    winreg.DeleteValue(key, k)
                elif isinstance(v, str):
                    winreg.SetValueEx(key, k, 0, winreg.REG_SZ, v)
                else:
                    errors.append('cannot write {} to registry'.format(type(v))) 
Example #3
Source File: env.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _winreg_read(self, base, path, key):
        try:
            if self.is_win_x64:
                hkey = winreg.CreateKeyEx(base, path, 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
            else:
                hkey = winreg.CreateKeyEx(base, path, 0, winreg.KEY_READ)

            value = winreg.QueryValueEx(hkey, key)
            return value

        except OSError:
            return None 
Example #4
Source File: env.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _winreg_read(self, base, path, key):
        try:
            if self.is_win_x64:
                hkey = winreg.CreateKeyEx(base, path, 0, winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
            else:
                hkey = winreg.CreateKeyEx(base, path, 0, winreg.KEY_READ)

            value = winreg.QueryValueEx(hkey, key)
            return value

        except OSError:
            return None 
Example #5
Source File: _registry.py    From pipenv with MIT License 5 votes vote down vote up
def set_value(self, value_name, value):
        with winreg.CreateKeyEx(self._root, self.subkey, 0, winreg.KEY_WRITE | self._flags) as key:
            if value is None:
                winreg.DeleteValue(key, value_name)
            elif isinstance(value, str):
                winreg.SetValueEx(key, value_name, 0, winreg.REG_SZ, value)
            else:
                raise TypeError('cannot write {} to registry'.format(type(value))) 
Example #6
Source File: _registry.py    From pipenv with MIT License 5 votes vote down vote up
def _set_all_values(self, rootkey, name, info, errors):
        with winreg.CreateKeyEx(rootkey, name, 0, winreg.KEY_WRITE | self._flags) as key:
            for k, v in info:
                if isinstance(v, PythonWrappedDict):
                    self._set_all_values(key, k, v._items(), errors)
                elif isinstance(v, dict):
                    self._set_all_values(key, k, v.items(), errors)
                elif v is None:
                    winreg.DeleteValue(key, k)
                elif isinstance(v, str):
                    winreg.SetValueEx(key, k, 0, winreg.REG_SZ, v)
                else:
                    errors.append('cannot write {} to registry'.format(type(v))) 
Example #7
Source File: Crypter.py    From Crypter with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #8
Source File: rpath.py    From r-bridge-install with Apache License 2.0 4 votes vote down vote up
def r_reg_write_value(r_key=None, r_value=None):
    """Write R registry values."""
    # keys to write
    r_write_keys = ('InstallPath', 'Current Version')
    if r_key not in r_write_keys:
        log.warn("asked to write an invalid key, {}".format(r_key))
        return None

    root_keys = OrderedDict((
        # try HKLM, then HKCU
        ('HKLM', winreg.HKEY_LOCAL_MACHINE),
        ('HKCU', winreg.HKEY_CURRENT_USER)
    ))
    # only work with the R and R64 hives, ArcGIS doesn't examine R32
    r_reg_paths = ["SOFTWARE\\R-core\\R",
                   "SOFTWARE\\R-core\\R64",
                   "SOFTWARE\\Wow6432Node\\R-Core\\R",
                   "SOFTWARE\\Wow6432Node\\R-Core\\R64"]

    for (key_name, root_key) in list(root_keys.items()):
        wrote = False
        for r_path in r_reg_paths:
            r_reg = None

            try:
                log.info("CreateKeyEx on {}\\{}, with write".format(
                    key_name, r_path))
                r_reg = winreg.CreateKeyEx(root_key, r_path, 0, FULL_ACCESS)
            except WindowsError as error:
                if error.errno == errno.ENOENT:
                    pass
                # permission denied, skip
                if error.errno == errno.EACCES:
                    log.debug("permission denied.")
                    continue

            if r_reg:
                try:
                    log.info('setting "{}" to "{}"'.format(r_key, r_value))
                    winreg.SetValueEx(r_reg, r_key, 0,
                                      winreg.REG_SZ, r_value)
                    wrote = True
                except fnf_exception as error:
                    handle_fnf(error)
        # only enter the keys into one hive
        if wrote:
            break