Description Field in WinXP Services

R

rbt

How does one associate a "Description" with a Windows service written in
Python? I've just started experimenting with Python services. Here's my
code... copied straight from Mr. Hammond's "Python Programming on Win32":

import win32serviceutil
import win32service
import win32event

class test_py_service(win32serviceutil.ServiceFramework):
_svc_name_ = "test_py_service"
## Tried the following to no avail.
_svc_description_ = "Test written by Brad"
_svc_display_name_ = "Test Python Service"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
win32event.WaitForSingleObject(self.hWaitStop,
win32event.INFINITE)


if __name__ == '__main__':
win32serviceutil.HandleCommandLine(test_py_service)
 
R

Roger Upole

ChangeServiceConfig2 is the api functions that sets the description,
but it's not in the win32service module (yet).

Roger
 
R

rbt

Roger said:
ChangeServiceConfig2 is the api functions that sets the description,
but it's not in the win32service module (yet).

Roger

OK, I can use _winreg to add the 'Description' field under the
appropriate registry key.
 
R

rbt

rbt said:
OK, I can use _winreg to add the 'Description' field under the
appropriate registry key.

Here's an example of it... kludgey but it works ;)

from _winreg import *
import time

def Svc_Description():
try:
key_location = r"SYSTEM\CurrentControlSet\Services\test_py_service"
svc_key = OpenKey(HKEY_LOCAL_MACHINE, key_location, 0, KEY_ALL_ACCESS)
SetValueEx(svc_key,'Description',0,REG_SZ,u"Brad's Test Python Service")
CloseKey(svc_key)
except Exception, e:
print e

Svc_Description()
time.sleep(10)
 
L

Larry Bates

Roger,

I wrote an extension to Mark Hammond's win32serviceutil.ServiceFramework class
(that I submitted to him also) that extends the class to provide you with this
functionality (along with the ability to support py2exe frozen services better).

Here it is:

import _winreg
import os
import win32evtlogutil

class NTserviceBase:
'''
Written by: Larry A. Bates - Syscon, Inc. March 2004

This is a base class for creating new NTservices with Python.
It should be included when you are defining your class as follows:

class newNTservice(win32serviceutil.ServiceFramework, NTserviceBase):
#
# Required attributes for a service
#
_svc_name_="newNTservice"
_svc_display_name_="newNTservice"
_svc_description_='newNTservice is a service that can be installed ' \
'and 'run on any Windows NT, 2000, XP, 2003 ' \

'computer. It can be controlled by services applet' \
'in the Control Panel and will run unattended ' \
'after being started.'

def __init__(self, args):
NTserviceBase.__init__(self)
win32serviceutil.ServiceFramework.__init__(self, args)
#
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
#
self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
return

Note: the __init__ method of the created class should call the __init__
method of NTserviceBase AND win32serviceutil.ServiceFramework to get
everything initialized properly
'''
def __init__(self):
#-------------------------------------------------------------------
# Call function to set self.PythonService (0=binary, 1=Python)
#-------------------------------------------------------------------
self.getSERVICEtype()
self.installpath=self.getSERVICEpath()
#-------------------------------------------------------------------
# Call function to set Service's long description string. Can't
# seem to find a way to do this that works for both PythonServices
# and binary (py2exe) services. So I will set the description
# every time the service is started.
#-------------------------------------------------------------------
self.setSERVICEdescription()
#-------------------------------------------------------------------
# Call function to register eventlog message handler which is
# different if I'm a binary vs. PythonService.
#-------------------------------------------------------------------
self.getPythonServicePath()
#-------------------------------------------------------------------
# If this is a PythonService, point to installed PythonService.exe
# if not, point to a copy of PythonService.exe that gets installed
# along with the binary. Note: on binary distributions you must
# include a copy of PythonService.exe along with your other files
# to provide EventLog message decoding.
#-------------------------------------------------------------------
if self.PythonService: sourcepath=self.PythonServicePath
else: sourcepath=os.path.join(self.installpath, "PythonService.exe ")
self.setSERVICEeventlogSource(sourcepath)
return

def getSERVICEtype(self):
'''
Function to determine if this is a Python service or a binary service
created by py2exe. Sets self.PythonService=1 if it is a Python service
or PythonService=0 if it is a binary service.
'''
#---------------------------------------------------------------------
# This service can run as a PythonService or it can run as a binary
# .EXE service (created by py2exe). We can sense this by looking
# to see if
# HKLM\SYSTEM\CurrentControlSet\Services\_svc_name_\PythonClass
# key exists in the registry. If it doesn't, then I'm a binary
# install.
#---------------------------------------------------------------------
# Open registry and search for PythonService install
#---------------------------------------------------------------------
regkey='SYSTEM\CurrentControlSet\Services\%s' % self._svc_name_
key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey)
nsubkeys, nvalues, lastmodified=_winreg.QueryInfoKey(key)
#---------------------------------------------------------------------
# Begin by assuming I'm a binary (non-Python) service created by py2exe
#---------------------------------------------------------------------
self.PythonService=0
#---------------------------------------------------------------------
# Loop over the keys in this branch looking for "PythonService" key
#---------------------------------------------------------------------
for i in range(nsubkeys):
subkeyname=_winreg.EnumKey(key, i)
if subkeyname == 'PythonClass':
self.PythonService=1
break

return

def getSERVICEpath(self):
'''
Function to retrieve the full pathname to where the service has been
installed on the machine. This can be used to locate .INI or other
data files (if this service uses any).
'''
if self.PythonService:
regkey='SYSTEM\CurrentControlSet\Services\%s\PythonClass' \
% self._svc_name_
value_name='' # Default
else:
regkey='SYSTEM\CurrentControlSet\Services\%s' % self._svc_name_
value_name="ImagePath"

#---------------------------------------------------------------------
# Get the contents of the value_name value under this key. This
# contains the full pathname where the service was installed and
# points me to the .INI file.
#---------------------------------------------------------------------
key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey)
SERVICEpath=_winreg.QueryValueEx(key, value_name)[0]
#---------------------------------------------------------------------
# Extract the full pathname where this service has been installed from
# this registry entry so I can locate the .INI file.
#---------------------------------------------------------------------
SERVICEpath=str(os.path.split(SERVICEpath)[0])
return SERVICEpath

def getPythonServicePath(self):
'''
Function to retrieve the full pathname to where PythonService.exe has
been installed on the machine (only used for Python Services, not binary
ones). This can be used to locate PythonService.exe for EventLog
message decoding.
'''
self.PythonService=None
regkey='SOFTWARE\Python\PythonService'
try: key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey)
except: return
#---------------------------------------------------------------------
# Get first subkeyname (Python Version)
#---------------------------------------------------------------------
subkeyname=_winreg.EnumKey(key, 0)
#---------------------------------------------------------------------
# Get the contents of the value_name value under this key. This
# contains the full pathname where the PythonService.exe is installed.
#---------------------------------------------------------------------
self.PythonServicePath=_winreg.QueryValue(key, subkeyname)
return

def setSERVICEdescription(self):
'''
This function sets the long description string that is displayed in the
Control Panel applet when this service is selected to the contents of
self._svc_description_ defined at the top of the service.
'''
#---------------------------------------------------------------------
# Open registry at the proper key
#---------------------------------------------------------------------
regkey='SYSTEM\CurrentControlSet\Services\%s' % self._svc_name_
key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey, 0, \
_winreg.KEY_SET_VALUE)

_winreg.SetValueEx(key, 'Description', 0, _winreg.REG_SZ, \
self._svc_description_)
return

def setSERVICEeventlogSource(self, sourcepath):
'''
This function sets the path to PythonService.exe so that EventLog
messages can be properly decoded.
'''
win32evtlogutil.AddSourceToRegistry(self._svc_name_, sourcepath,
'Application')
return


Hope it helps,
Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top