我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用win32api.RegSetValue()。
def RegisterPythonExe(exeFullPath, exeAlias = None, exeAppPath = None): """Register a .exe file that uses Python. Registers the .exe with the OS. This allows the specified .exe to be run from the command-line or start button without using the full path, and also to setup application specific path (ie, os.environ['PATH']). Currently the exeAppPath is not supported, so this function is general purpose, and not specific to Python at all. Later, exeAppPath may provide a reasonable default that is used. exeFullPath -- The full path to the .exe exeAlias = None -- An alias for the exe - if none, the base portion of the filename is used. exeAppPath -- Not supported. """ # Note - Dont work on win32s (but we dont care anymore!) if exeAppPath: raise error("Do not support exeAppPath argument currently") if exeAlias is None: exeAlias = os.path.basename(exeFullPath) win32api.RegSetValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias, win32con.REG_SZ, exeFullPath)
def RegisterModule(modName, modPath): """Register an explicit module in the registry. This forces the Python import mechanism to locate this module directly, without a sys.path search. Thus a registered module need not appear in sys.path at all. modName -- The name of the module, as used by import. modPath -- The full path and file name of the module. """ try: import os os.stat(modPath) except os.error: print "Warning: Registering non-existant module %s" % modPath win32api.RegSetValue(GetRootKey(), BuildDefaultPythonKey() + "\\Modules\\%s" % modName, win32con.REG_SZ, modPath)
def RegisterHelpFile(helpFile, helpPath, helpDesc = None, bCheckFile = 1): """Register a help file in the registry. Note that this used to support writing to the Windows Help key, however this is no longer done, as it seems to be incompatible. helpFile -- the base name of the help file. helpPath -- the path to the help file helpDesc -- A description for the help file. If None, the helpFile param is used. bCheckFile -- A flag indicating if the file existence should be checked. """ if helpDesc is None: helpDesc = helpFile fullHelpFile = os.path.join(helpPath, helpFile) try: if bCheckFile: os.stat(fullHelpFile) except os.error: raise ValueError("Help file does not exist") # Now register with Python itself. win32api.RegSetValue(GetRootKey(), BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc, win32con.REG_SZ, fullHelpFile)
def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand): """Register the core Python file extensions. defPyIcon -- The default icon to use for .py files, in 'fname,offset' format. defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format. runCommand -- The command line to use for running .py files """ # Register the file extensions. pythonFileId = RegistryIDPyFile win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".py", win32con.REG_SZ, pythonFileId) win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Python File") win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\CLSID" % pythonFileId , win32con.REG_SZ, CLSIDPyFile) win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPyIcon) base = "%s\\Shell" % RegistryIDPyFile win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run") win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand) # Register the .PYC. pythonFileId = RegistryIDPycFile win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".pyc", win32con.REG_SZ, pythonFileId) win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Compiled Python File") win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPycIcon) base = "%s\\Shell" % pythonFileId win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run") win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
def testNotifyChange(self): def change(): hkey = win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, self.key_name) try: win32api.RegSetValue(hkey, None, win32con.REG_SZ, "foo") finally: win32api.RegDeleteKey(win32con.HKEY_CURRENT_USER, self.key_name) evt = win32event.CreateEvent(None,0,0,None) ## REG_NOTIFY_CHANGE_LAST_SET - values ## REG_CHANGE_NOTIFY_NAME - keys ## REG_NOTIFY_CHANGE_SECURITY - security descriptor ## REG_NOTIFY_CHANGE_ATTRIBUTES win32api.RegNotifyChangeKeyValue(win32con.HKEY_CURRENT_USER,1,win32api.REG_NOTIFY_CHANGE_LAST_SET,evt,True) ret_code=win32event.WaitForSingleObject(evt,0) # Should be no change. self.failUnless(ret_code==win32con.WAIT_TIMEOUT) change() # Our event should now be in a signalled state. ret_code=win32event.WaitForSingleObject(evt,0) self.failUnless(ret_code==win32con.WAIT_OBJECT_0)
def RegisterCoreDLL(coredllName = None): """Registers the core DLL in the registry. If no params are passed, the name of the Python DLL used in the current process is used and registered. """ if coredllName is None: coredllName = win32api.GetModuleFileName(sys.dllhandle) # must exist! else: try: os.stat(coredllName) except os.error: print "Warning: Registering non-existant core DLL %s" % coredllName hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey()) try: win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName) finally: win32api.RegCloseKey(hKey) # Lastly, setup the current version to point to me. win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
def RegisterCoreDLL(coredllName = None): """Registers the core DLL in the registry. If no params are passed, the name of the Python DLL used in the current process is used and registered. """ if coredllName is None: coredllName = win32api.GetModuleFileName(sys.dllhandle) # must exist! else: try: os.stat(coredllName) except os.error: print("Warning: Registering non-existant core DLL %s" % coredllName) hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey()) try: win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName) finally: win32api.RegCloseKey(hKey) # Lastly, setup the current version to point to me. win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
def SetRegistryDefaultValue(subKey, value, rootkey = None): """A helper to set the default value for a key in the registry """ if rootkey is None: rootkey = GetRootKey() if type(value)==str: typeId = win32con.REG_SZ elif type(value)==int: typeId = win32con.REG_DWORD else: raise TypeError("Value must be string or integer - was passed " + repr(value)) win32api.RegSetValue(rootkey, subKey, typeId ,value)
def RegisterNamedPath(name, path): """Register a named path - ie, a named PythonPath entry. """ keyStr = BuildDefaultPythonKey() + "\\PythonPath" if name: keyStr = keyStr + "\\" + name win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand = None): # Last param for "Open" - for a .py file to be executed by the command line # or shell execute (eg, just entering "foo.py"), the Command must be "Open", # but you may associate a different name for the right-click menu. # In our case, normally we have "Open=Run" base = "%s\\Shell" % RegistryIDPyFile if shellUserCommand: win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s" % (shellCommand), win32con.REG_SZ, shellUserCommand) win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\Command" % (shellCommand), win32con.REG_SZ, exeCommand)
def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand): base = "%s\\Shell" % RegistryIDPyFile win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec" % (shellCommand), win32con.REG_SZ, ddeCommand) win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Application" % (shellCommand), win32con.REG_SZ, ddeApp) win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Topic" % (shellCommand), win32con.REG_SZ, ddeTopic)
def InstallPythonClassString(pythonClassString, serviceName): # Now setup our Python specific entries. if pythonClassString: key = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\%s\\PythonClass" % serviceName) try: win32api.RegSetValue(key, None, win32con.REG_SZ, pythonClassString); finally: win32api.RegCloseKey(key) # Utility functions for Services, to allow persistant properties.
def _set_string(path, value, base=win32con.HKEY_CLASSES_ROOT): "Set a string value in the registry." win32api.RegSetValue(base, path, win32con.REG_SZ, value)
def OnAddValue(self,command, code): from pywin.mfc import dialog val = dialog.GetSimpleInput("New value", "", "Add new value") if val is None: return # cancelled. hitem = self.hierList.GetSelectedItem() item = self.hierList.ItemFromHandle(hitem) if SafeApply(win32api.RegSetValue, (item.keyRoot, item.keyName, win32con.REG_SZ, val)): # Simply re-select the current item to refresh the right spitter. self.PerformItemSelected(item) # self.Select(hitem, commctrl.TVGN_CARET)
def SetupCore(searchPaths): """Setup the core Python information in the registry. This function makes no assumptions about the current state of sys.path. After this function has completed, you should have access to the standard Python library, and the standard Win32 extensions """ import sys for path in searchPaths: sys.path.append(path) import os import regutil, win32api,win32con installPath, corePaths = LocatePythonCore(searchPaths) # Register the core Pythonpath. print corePaths regutil.RegisterNamedPath(None, ';'.join(corePaths)) # Register the install path. hKey = win32api.RegCreateKey(regutil.GetRootKey() , regutil.BuildDefaultPythonKey()) try: # Core Paths. win32api.RegSetValue(hKey, "InstallPath", win32con.REG_SZ, installPath) finally: win32api.RegCloseKey(hKey) # Register the win32 core paths. win32paths = os.path.abspath( os.path.split(win32api.__file__)[0]) + ";" + \ os.path.abspath( os.path.split(LocateFileName("win32con.py;win32con.pyc", sys.path ) )[0] ) # Python has builtin support for finding a "DLLs" directory, but # not a PCBuild. Having it in the core paths means it is ignored when # an EXE not in the Python dir is hosting us - so we add it as a named # value check = os.path.join(sys.prefix, "PCBuild") if "64 bit" in sys.version: check = os.path.join(check, "amd64") if os.path.isdir(check): regutil.RegisterNamedPath("PCBuild",check)
def SetupCore(searchPaths): """Setup the core Python information in the registry. This function makes no assumptions about the current state of sys.path. After this function has completed, you should have access to the standard Python library, and the standard Win32 extensions """ import sys for path in searchPaths: sys.path.append(path) import os import regutil, win32api,win32con installPath, corePaths = LocatePythonCore(searchPaths) # Register the core Pythonpath. print(corePaths) regutil.RegisterNamedPath(None, ';'.join(corePaths)) # Register the install path. hKey = win32api.RegCreateKey(regutil.GetRootKey() , regutil.BuildDefaultPythonKey()) try: # Core Paths. win32api.RegSetValue(hKey, "InstallPath", win32con.REG_SZ, installPath) finally: win32api.RegCloseKey(hKey) # Register the win32 core paths. win32paths = os.path.abspath( os.path.split(win32api.__file__)[0]) + ";" + \ os.path.abspath( os.path.split(LocateFileName("win32con.py;win32con.pyc", sys.path ) )[0] ) # Python has builtin support for finding a "DLLs" directory, but # not a PCBuild. Having it in the core paths means it is ignored when # an EXE not in the Python dir is hosting us - so we add it as a named # value check = os.path.join(sys.prefix, "PCBuild") if "64 bit" in sys.version: check = os.path.join(check, "amd64") if os.path.isdir(check): regutil.RegisterNamedPath("PCBuild",check)