473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Description Field in WinXP Services

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 )

Jul 18 '05 #1
4 2212
ChangeServiceConfig2 is the api functions that sets the description,
but it's not in the win32service module (yet).

Roger

"rbt" <rb*@athop1.ath.vt.edu> wrote in message
news:ct**********@solaris.cc.vt.edu...
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 =---
Jul 18 '05 #2
rbt
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.
Jul 18 '05 #3
rbt
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_servic e"
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)
Jul 18 '05 #4
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\Pytho nClass' \
% 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_servic e"
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)

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Larry Bates | last post by:
Anybody know what variable I need to define in my class to set the long description that shows up in the Services control panel applet? _svc_name_ holds the short name...
3
by: Matthew P. Seltzer | last post by:
All- This is a bit 'out there,' but, here goes. I have copied the WinSxS folder from a WinXP box to my Win2K box. I would like to be able to apply XP control styles to my Win2K VB.NET apps. Given...
2
by: Ralph | last post by:
I used to have Visual Basic .net std. 2003 installed on WinXP SP1A. But I found it too hard to upgrade WinXP to SP2. Now, I do have WinXP SP2 installed, but I am having problems installing...
3
by: sparks | last post by:
I was copying fields from one table to another. IF the var name starts with milk I change it to egg and create it in the destination table. It works fine but I want to copy the description as...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
2
by: Per Rollvang | last post by:
Hi All! I am trying to figure out how to get info about different services from C#.NET. If I open the Services Applet from Control Panel (WinXP), I have an 'Extended Tab', whitch gives me a...
2
by: WRH | last post by:
Hello When I look at windows services with the Windows Administration tools Services utility (Win XP) I note that many services have a description field (2nd column). How can I add such a...
1
by: Dave | last post by:
I wrote a C# windows service and it will install on Win2K but won't start. I have code in it to write to the event log but it doesn't log anything. I developed it on WinXP with VS and it...
0
by: jcvoon | last post by:
Hi all: The application is a WinForm application which will invoke the Web Services host in the same development machine, the WSE policy is setup properly and it is configure to use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.