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 _set_subkeys(keyName, valueDict, base=win32con.HKEY_CLASSES_ROOT): hkey = win32api.RegCreateKey(base, keyName) try: for key, value in valueDict.items(): win32api.RegSetValueEx(hkey, key, None, win32con.REG_SZ, value) finally: win32api.RegCloseKey(hkey)
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)
def InstallPerfmonForService(serviceName, iniName, dllName=None): # If no DLL name, look it up in the INI file name if not dllName: # May be empty string! dllName = win32api.GetProfileVal("Python", "dll", "", iniName) # Still not found - look for the standard one in the same dir as # win32service.pyd if not dllName: try: tryName = os.path.join( os.path.split(win32service.__file__)[0], "perfmondata.dll") if os.path.isfile(tryName): dllName = tryName except AttributeError: # Frozen app? - anyway, can't find it! pass if not dllName: raise ValueError("The name of the performance DLL must be available") dllName = win32api.GetFullPathName(dllName) # Now setup all the required "Performance" entries. hkey = win32api.RegOpenKey( win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\%s" % (serviceName), 0, win32con.KEY_ALL_ACCESS) try: subKey = win32api.RegCreateKey(hkey, "Performance") try: win32api.RegSetValueEx(subKey, "Library", 0, win32con.REG_SZ, dllName) win32api.RegSetValueEx(subKey, "Open", 0, win32con.REG_SZ, "OpenPerformanceData") win32api.RegSetValueEx(subKey, "Close", 0, win32con.REG_SZ, "ClosePerformanceData") win32api.RegSetValueEx(subKey, "Collect", 0, win32con.REG_SZ, "CollectPerformanceData") finally: win32api.RegCloseKey(subKey) finally: win32api.RegCloseKey(hkey) # Now do the "Lodctr" thang... try: import perfmon path, fname = os.path.split(iniName) oldPath = os.getcwd() if path: os.chdir(path) try: perfmon.LoadPerfCounterTextStrings("python.exe " + fname) finally: os.chdir(oldPath) except win32api.error as details: print("The service was installed OK, but the performance monitor") print("data could not be loaded.", details)
def AddSourceToRegistry(appName, msgDLL=None, eventLogType="Application", eventLogFlags=None): """Add a source of messages to the event log. Allows Python program to register a custom source of messages in the registry. You must also provide the DLL name that has the message table, so the full message text appears in the event log. Note that the win32evtlog.pyd file has a number of string entries with just "%1" built in, so many Python programs can simply use this DLL. Disadvantages are that you do not get language translation, and the full text is stored in the event log, blowing the size of the log up. """ # When an application uses the RegisterEventSource or OpenEventLog # function to get a handle of an event log, the event loggging service # searches for the specified source name in the registry. You can add a # new source name to the registry by opening a new registry subkey # under the Application key and adding registry values to the new # subkey. if msgDLL is None: msgDLL = win32evtlog.__file__ # Create a new key for our application hkey = win32api.RegCreateKey( win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" % (eventLogType, appName)) # Add the Event-ID message-file name to the subkey. win32api.RegSetValueEx( hkey, "EventMessageFile", # value name \ 0, # reserved \ win32con.REG_EXPAND_SZ, # value type \ msgDLL) # Set the supported types flags and add it to the subkey. if eventLogFlags is None: eventLogFlags = win32evtlog.EVENTLOG_ERROR_TYPE | win32evtlog.EVENTLOG_WARNING_TYPE | win32evtlog.EVENTLOG_INFORMATION_TYPE win32api.RegSetValueEx( hkey, # subkey handle \ "TypesSupported", # value name \ 0, # reserved \ win32con.REG_DWORD, # value type \ eventLogFlags) win32api.RegCloseKey(hkey)
def SetServiceCustomOption(serviceName, option, value): try: serviceName = serviceName._svc_name_ except AttributeError: pass key = win32api.RegCreateKey( win32con.HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\%s\\Parameters" % serviceName) try: if isinstance(value, type(0)): win32api.RegSetValueEx(key, option, 0, win32con.REG_DWORD, value) else: win32api.RegSetValueEx(key, option, 0, win32con.REG_SZ, value) finally: win32api.RegCloseKey(key)
def GetServiceCustomOption(serviceName, option, defaultValue=None): # First param may also be a service class/instance. # This allows services to pass "self" try: serviceName = serviceName._svc_name_ except AttributeError: pass key = win32api.RegCreateKey( win32con.HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\%s\\Parameters" % serviceName) try: try: return win32api.RegQueryValueEx(key, option)[0] except win32api.error: # No value. return defaultValue finally: win32api.RegCloseKey(key)