我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用winreg.HKEY_CLASSES_ROOT。
def remove_jupyter_here(): for env in ('qtconsole', 'notebook'): try: winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\shell\jupyter_%s_here\Command' % env) winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\shell\jupyter_%s_here' % env) winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\Background\shell\jupyter_%s_here\Command' % env) winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\Background\shell\jupyter_%s_here' % env) print("Jupyter %s here context menu entry removed." % env) except: # If this fails it is because it was not installed, so nothing to # worry about. pass
def RegisterPythonServer(filename, progids=None, verbose=0): if progids: if isinstance(progids, str): 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)