Description Field in WinXP Services

Discussion in 'Python' started by rbt, Jan 29, 2005.

  1. rbt

    rbt Guest

    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)
     
    rbt, Jan 29, 2005
    #1
    1. Advertising

  2. rbt

    Roger Upole Guest

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

    Roger

    "rbt" <> wrote in message
    news:ctgij1$diq$...
    > 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)
    >





    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
     
    Roger Upole, Jan 29, 2005
    #2
    1. Advertising

  3. rbt

    rbt Guest

    Roger Upole wrote:
    > 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.
     
    rbt, Jan 30, 2005
    #3
  4. rbt

    rbt Guest

    rbt wrote:
    > Roger Upole wrote:
    >
    >> 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.


    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)
     
    rbt, Jan 30, 2005
    #4
  5. rbt

    Larry Bates Guest

    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



    rbt wrote:
    > rbt wrote:
    >
    >> Roger Upole wrote:
    >>
    >>> 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.

    >
    >
    > 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)
     
    Larry Bates, Jan 31, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pete
    Replies:
    1
    Views:
    2,945
    Jon Yates
    Aug 29, 2003
  2. Mark B
    Replies:
    2
    Views:
    2,291
    Mark B
    May 7, 2009
  3. Chris Bardon

    Parsing WSDL and System.Web.Services.Description

    Chris Bardon, May 17, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    187
    Chris Bardon
    May 17, 2004
  4. Sound
    Replies:
    2
    Views:
    454
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    426
Loading...

Share This Page