def GetExtensionInfo(pExtension):
lHandle = reg.OpenKey(reg.HKEY_CLASSES_ROOT, pExtension)
lSubKeys, lValueCount, lModified = reg.QueryInfoKey(lHandle)
lValues = {}
for lCount in range(0, lValueCount):
lName, lValue, lType = reg.EnumValue(lHandle, lCount)
if lName == '':
lName = "File Type"
lValues[lName] =lValue
lKey = lValues['File Type']
lDescription = reg.QueryValue(reg.HKEY_CLASSES_ROOT, lKey)
lValues['Description'] = lDescription
lKey += '\\Shell'
lDefault = reg.QueryValue(reg.HKEY_CLASSES_ROOT, lKey)
lValues['Default'] = lDefault
return lValues
python类QueryValue()的实例源码
def get_registry_app_path(key, filename):
if not winreg:
return None
try:
result = winreg.QueryValue(key, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s.exe" % filename[0])
except WindowsError:
pass
else:
if os.path.isfile(result):
return result
def _find_exe_in_registry(self):
try:
from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
except ImportError:
from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
import shlex
keys = (
r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command"
)
command = ""
for path in keys:
try:
key = OpenKey(HKEY_LOCAL_MACHINE, path)
command = QueryValue(key, "")
break
except OSError:
try:
key = OpenKey(HKEY_CURRENT_USER, path)
command = QueryValue(key, "")
break
except OSError:
pass
else:
return ""
if not command:
return ""
return shlex.split(command)[0]
def _find_exe_in_registry(self):
try:
from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
except ImportError:
from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
import shlex
keys = (
r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command"
)
command = ""
for path in keys:
try:
key = OpenKey(HKEY_LOCAL_MACHINE, path)
command = QueryValue(key, "")
break
except OSError:
try:
key = OpenKey(HKEY_CURRENT_USER, path)
command = QueryValue(key, "")
break
except OSError:
pass
else:
return ""
if not command:
return ""
return shlex.split(command)[0]
def _get_awvs_console_path(self):
"""Return
"""
try:
conn = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
wvs_path = _winreg.QueryValue(conn, 'SOFTWARE\Classes\Acunetix_WVS_Scan\Shell\Open\Command')
_winreg.CloseKey(conn)
wvs_path = re.search('"([^"]*)"', wvs_path).group(1)
wvs_dir = os.path.dirname(wvs_path)
return os.path.join(wvs_dir, 'wvs_console.exe')
except Exception, e:
LOG.error(e, exc_info=True)
def _find_exe_in_registry(self):
try:
from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
except ImportError:
from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
import shlex
keys = (r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command")
command = ""
for path in keys:
try:
key = OpenKey(HKEY_LOCAL_MACHINE, path)
command = QueryValue(key, "")
break
except OSError:
try:
key = OpenKey(HKEY_CURRENT_USER, path)
command = QueryValue(key, "")
break
except OSError:
pass
else:
return ""
if not command:
return ""
return shlex.split(command)[0]
def _find_exe_in_registry(self):
try:
from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
except ImportError:
from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
import shlex
keys = (
r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command"
)
command = ""
for path in keys:
try:
key = OpenKey(HKEY_LOCAL_MACHINE, path)
command = QueryValue(key, "")
break
except OSError:
try:
key = OpenKey(HKEY_CURRENT_USER, path)
command = QueryValue(key, "")
break
except OSError:
pass
else:
return ""
if not command:
return ""
return shlex.split(command)[0]
def get_installed_pythons():
try:
python_core = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE,
"Software\\Python\\PythonCore")
except WindowsError:
# No registered Python installations
return {}
i = 0
versions = []
while True:
try:
versions.append(winreg.EnumKey(python_core, i))
i = i + 1
except WindowsError:
break
exes = dict()
for ver in versions:
try:
path = winreg.QueryValue(python_core, "%s\\InstallPath" % ver)
except WindowsError:
continue
exes[ver] = join(path, "python.exe")
winreg.CloseKey(python_core)
# Add the major versions
# Sort the keys, then repeatedly update the major version entry
# Last executable (i.e., highest version) wins with this approach
for ver in sorted(exes):
exes[ver[0]] = exes[ver]
return exes
def get_installed_pythons():
try:
python_core = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE,
"Software\\Python\\PythonCore")
except WindowsError:
# No registered Python installations
return {}
i = 0
versions = []
while True:
try:
versions.append(winreg.EnumKey(python_core, i))
i = i + 1
except WindowsError:
break
exes = dict()
for ver in versions:
try:
path = winreg.QueryValue(python_core, "%s\\InstallPath" % ver)
except WindowsError:
continue
exes[ver] = join(path, "python.exe")
winreg.CloseKey(python_core)
# Add the major versions
# Sort the keys, then repeatedly update the major version entry
# Last executable (i.e., highest version) wins with this approach
for ver in sorted(exes):
exes[ver[0]] = exes[ver]
return exes
def get_installed_pythons():
try:
python_core = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE,
"Software\\Python\\PythonCore")
except WindowsError:
# No registered Python installations
return {}
i = 0
versions = []
while True:
try:
versions.append(winreg.EnumKey(python_core, i))
i = i + 1
except WindowsError:
break
exes = dict()
for ver in versions:
try:
path = winreg.QueryValue(python_core, "%s\\InstallPath" % ver)
except WindowsError:
continue
exes[ver] = join(path, "python.exe")
winreg.CloseKey(python_core)
# Add the major versions
# Sort the keys, then repeatedly update the major version entry
# Last executable (i.e., highest version) wins with this approach
for ver in sorted(exes):
exes[ver[0]] = exes[ver]
return exes
def get_shortcuts_folder():
if get_root_hkey()==winreg.HKEY_LOCAL_MACHINE:
try:
fldr = get_special_folder_path("CSIDL_COMMON_PROGRAMS")
except OSError:
# No CSIDL_COMMON_PROGRAMS on this platform
fldr = get_special_folder_path("CSIDL_PROGRAMS")
else:
# non-admin install - always goes in this user's start menu.
fldr = get_special_folder_path("CSIDL_PROGRAMS")
try:
install_group = winreg.QueryValue(get_root_hkey(),
root_key_name + "\\InstallPath\\InstallGroup")
except OSError:
vi = sys.version_info
install_group = "Python %d.%d" % (vi[0], vi[1])
return os.path.join(fldr, install_group)
# Get the system directory, which may be the Wow64 directory if we are a 32bit
# python on a 64bit OS.
def _find_exe_in_registry(self):
try:
from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
except ImportError:
from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
import shlex
keys = (r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command")
command = ""
for path in keys:
try:
key = OpenKey(HKEY_LOCAL_MACHINE, path)
command = QueryValue(key, "")
break
except OSError:
try:
key = OpenKey(HKEY_CURRENT_USER, path)
command = QueryValue(key, "")
break
except OSError:
pass
else:
return ""
if not command:
return ""
return shlex.split(command)[0]
def _find_exe_in_registry(self):
try:
from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
except ImportError:
from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
import shlex
keys = (
r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command"
)
command = ""
for path in keys:
try:
key = OpenKey(HKEY_LOCAL_MACHINE, path)
command = QueryValue(key, "")
break
except OSError:
try:
key = OpenKey(HKEY_CURRENT_USER, path)
command = QueryValue(key, "")
break
except OSError:
pass
else:
return ""
if not command:
return ""
return shlex.split(command)[0]
def get_inkscape_path():
""" Return the Inkscape path """
import _winreg
svgexepath = _winreg.QueryValue(_winreg.HKEY_LOCAL_MACHINE,
'Software\\Classes\\svgfile\\shell\\Inkscape\\command')
svgexepath = svgexepath.replace('"%1"', '')
return svgexepath.replace('"', '')
def get_acroversion():
" Return version of Adobe Acrobat executable or None"
import _winreg
adobesoft = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'Software\Adobe')
for index in range(_winreg.QueryInfoKey(adobesoft)[0]):
key = _winreg.EnumKey(adobesoft, index)
if "acrobat" in key.lower():
acrokey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'Software\\Adobe\\%s' % key)
for index in range(_winreg.QueryInfoKey(acrokey)[0]):
numver = _winreg.EnumKey(acrokey, index)
try:
res = _winreg.QueryValue(_winreg.HKEY_LOCAL_MACHINE, 'Software\\Adobe\\%s\\%s\\InstallPath' % (key, numver))
return res
except Exception:
pass
return None
def get_installed_pythons():
try:
python_core = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE,
"Software\\Python\\PythonCore")
except WindowsError:
# No registered Python installations
return {}
i = 0
versions = []
while True:
try:
versions.append(winreg.EnumKey(python_core, i))
i = i + 1
except WindowsError:
break
exes = dict()
for ver in versions:
try:
path = winreg.QueryValue(python_core, "%s\\InstallPath" % ver)
except WindowsError:
continue
exes[ver] = join(path, "python.exe")
winreg.CloseKey(python_core)
# Add the major versions
# Sort the keys, then repeatedly update the major version entry
# Last executable (i.e., highest version) wins with this approach
for ver in sorted(exes):
exes[ver[0]] = exes[ver]
return exes
def __set_values(self, values):
'Private class method.'
if isinstance(values, str):
_winreg.SetValueEx(self.__self, values, 0, REG.SZ, _winreg.QueryValue(self.__self, ''))
elif isinstance(values, (list, tuple)):
for value in values:
self.values = value
else:
raise TypeError, 'Value Could Not Be Created'
def __get_value(self):
'Private class method.'
return _winreg.QueryValue(self.__self, '')
def __set_values(self, values):
'Private class method.'
if isinstance(values, str):
_winreg.SetValueEx(self.__key, values, 0, REG.SZ, _winreg.QueryValue(self.__key, ''))
elif isinstance(values, (list, tuple)):
for value in values:
self.values = value
else:
raise TypeError, 'Value Could Not Be Created'
def __get_value(self):
'Private class method.'
return _winreg.QueryValue(self.__key, '')
def __set_values(self, values):
'Private class method.'
if isinstance(values, str):
_winreg.SetValueEx(self.__key, values, 0, REG.SZ, _winreg.QueryValue(self.__key, ''))
elif isinstance(values, (list, tuple)):
for value in values:
self.values = value
else:
raise TypeError, 'Value Could Not Be Created'
def getvalue(self, name=''):
""" Return the value of an item inside the current key,
given its name """
try:
if name:
return wreg.QueryValueEx(self._hkey, name)
else:
return wreg.QueryValue(self._hkey, '')
except (WindowsError, EnvironmentError), e:
raise RegistryError, e
def hasvalue(self, name=''):
""" Return True if the current key has a value named
'name', False otherwise """
try:
if name:
wreg.QueryValueEx(self._hkey, name)
else:
wreg.QueryValue(self._hkey, '')
return True
except (WindowsError, EnvironmentError), e:
return False
def GetCommand(pFileType, pCommandName):
lKey = pFileType + '\\Shell\\' + pCommandName + '\\Command'
print lKey
lCommand= reg.QueryValue(reg.HKEY_CLASSES_ROOT, lKey)
print lCommand
return lCommand
def get_registry_app_path(key, filename):
if not winreg:
return None
try:
result = winreg.QueryValue(key, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s.exe" % filename[0])
except WindowsError:
pass
else:
if os.path.isfile(result):
return result
def get_registry_app_path(key, filename):
if not winreg:
return None
try:
result = winreg.QueryValue(key, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s.exe" % filename[0])
except WindowsError:
pass
else:
if os.path.isfile(result):
return result
def RegisterPythonServer(filename, progids=None, verbose=0):
if progids:
if isinstance(progids, basestring):
progids = [progids]
# we know the CLSIDs we need, but we might not be an admin user
# and otherwise unable to register them. So as long as the progids
# exist and the DLL points at our version, assume it already is.
why_not = None
for progid in progids:
try:
clsid = pythoncom.MakeIID(progid)
except pythoncom.com_error:
# no progid - not registered.
break
# have a CLSID - open it.
try:
HKCR = _winreg.HKEY_CLASSES_ROOT
hk = _winreg.OpenKey(HKCR, "CLSID\\%s" % clsid)
dll = _winreg.QueryValue(hk, "InprocServer32")
except WindowsError:
# no CLSID or InProcServer32 - not good!
break
ok_files = [os.path.basename(pythoncom.__file__),
'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])]
if os.path.basename(dll) not in ok_files:
why_not = "%r is registered against a different Python version (%s)" % (progid, dll)
break
else:
#print "Skipping registration of '%s' - already registered" % filename
return
# needs registration - see if its likely!
try:
from win32com.shell.shell import IsUserAnAdmin
except ImportError:
print "Can't import win32com.shell - no idea if you are an admin or not?"
is_admin = False
else:
try:
is_admin = IsUserAnAdmin()
except pythoncom.com_error:
# old, less-secure OS - assume *is* admin.
is_admin = True
if not is_admin:
msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0]
if why_not:
msg += "\n(registration check failed as %s)" % why_not
# throw a normal "class not registered" exception - we don't report
# them the same way as "real" errors.
raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1)
# so theoretically we are able to register it.
cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename)
if verbose:
print "Registering engine", filename
# print cmd
rc = os.system(cmd)
if rc:
print "Registration command was:"
print cmd
raise RuntimeError("Registration of engine '%s' failed" % filename)
def RegisterPythonServer(filename, progids=None, verbose=0):
if progids:
if isinstance(progids, basestring):
progids = [progids]
# we know the CLSIDs we need, but we might not be an admin user
# and otherwise unable to register them. So as long as the progids
# exist and the DLL points at our version, assume it already is.
why_not = None
for progid in progids:
try:
clsid = pythoncom.MakeIID(progid)
except pythoncom.com_error:
# no progid - not registered.
break
# have a CLSID - open it.
try:
HKCR = _winreg.HKEY_CLASSES_ROOT
hk = _winreg.OpenKey(HKCR, "CLSID\\%s" % clsid)
dll = _winreg.QueryValue(hk, "InprocServer32")
except WindowsError:
# no CLSID or InProcServer32 - not good!
break
ok_files = [os.path.basename(pythoncom.__file__),
'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])]
if os.path.basename(dll) not in ok_files:
why_not = "%r is registered against a different Python version (%s)" % (progid, dll)
break
else:
#print "Skipping registration of '%s' - already registered" % filename
return
# needs registration - see if its likely!
try:
from win32com.shell.shell import IsUserAnAdmin
except ImportError:
print "Can't import win32com.shell - no idea if you are an admin or not?"
is_admin = False
else:
try:
is_admin = IsUserAnAdmin()
except pythoncom.com_error:
# old, less-secure OS - assume *is* admin.
is_admin = True
if not is_admin:
msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0]
if why_not:
msg += "\n(registration check failed as %s)" % why_not
# throw a normal "class not registered" exception - we don't report
# them the same way as "real" errors.
raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1)
# so theoretically we are able to register it.
cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename)
if verbose:
print "Registering engine", filename
# print cmd
rc = os.system(cmd)
if rc:
print "Registration command was:"
print cmd
raise RuntimeError("Registration of engine '%s' failed" % filename)
def RegisterPythonServer(filename, progids=None, verbose=0):
if progids:
if isinstance(progids, basestring):
progids = [progids]
# we know the CLSIDs we need, but we might not be an admin user
# and otherwise unable to register them. So as long as the progids
# exist and the DLL points at our version, assume it already is.
why_not = None
for progid in progids:
try:
clsid = pythoncom.MakeIID(progid)
except pythoncom.com_error:
# no progid - not registered.
break
# have a CLSID - open it.
try:
HKCR = _winreg.HKEY_CLASSES_ROOT
hk = _winreg.OpenKey(HKCR, "CLSID\\%s" % clsid)
dll = _winreg.QueryValue(hk, "InprocServer32")
except WindowsError:
# no CLSID or InProcServer32 - not good!
break
ok_files = [os.path.basename(pythoncom.__file__),
'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])]
if os.path.basename(dll) not in ok_files:
why_not = "%r is registered against a different Python version (%s)" % (progid, dll)
break
else:
#print "Skipping registration of '%s' - already registered" % filename
return
# needs registration - see if its likely!
try:
from win32com.shell.shell import IsUserAnAdmin
except ImportError:
print "Can't import win32com.shell - no idea if you are an admin or not?"
is_admin = False
else:
try:
is_admin = IsUserAnAdmin()
except pythoncom.com_error:
# old, less-secure OS - assume *is* admin.
is_admin = True
if not is_admin:
msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0]
if why_not:
msg += "\n(registration check failed as %s)" % why_not
# throw a normal "class not registered" exception - we don't report
# them the same way as "real" errors.
raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1)
# so theoretically we are able to register it.
cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename)
if verbose:
print "Registering engine", filename
# print cmd
rc = os.system(cmd)
if rc:
print "Registration command was:"
print cmd
raise RuntimeError("Registration of engine '%s' failed" % filename)