Problem listing services with wmi

Tim Golden tim.golden at viacom-outdoor.co.uk
Wed May 18 03:59:05 EDT 2005


[... snip my comparison of win32service & WMI ...]

| Seems like your hunch was right, the first loop completes correctly. 
| Adding a counter to both loops shows that getting services from 
| win32service.EnumServicesStatus() gets 108 services, and getting them 
| through WMI gets to 87 and then halts with the previously 
| mentioned error :
| 
| Traceback (most recent call last):
|   File "<stdin>", line 1, in ?
|   File 
| "G:\Python-2.4\Lib\site-packages\win32com\client\util.py", line 
| 83, in next
|     return _get_good_object_(self._iter_.next())
| pywintypes.com_error: (-2147217398, 'OLE error 0x8004100a', 
| None, None)
|  From examining your code I imagine you tried to catch the exception, 
| but it didn't work because the exception is thrown on the 
| "for" line and 
| not when getting the service's Caption property... 

Slightly altered version of the code below. This one
doesn't use pywin32's iterator interface, although it
does use the __getitem__ interface, so it may still
cause probs.

| Python 2.4 on Windows XP SP2 here.

OK. I'm running 2.3 normally but I've run the scripts with
2.4 so I don't think it makes a difference. I'll leave the
set import logic in place so I don't have to remember.

| Do you think we're getting anywhere with this? If not, then I'll just 
| use the "win32" method of getting the service list, even 
| though it isn't 
| as pretty. I don't want to waste your time on something that may have 
| more to do with my machine than anything else... But if you 
| want to try 
| and get to the bottom of this, I have no problem with trying 
| out other 
| suggestions.

It's obvious from the VBS error that it's WMI at fault rather than
Python as such. All I'm trying to do here is to deal gracefully with
the errors rather than falling over in a heap. It would be interesting
to know what it is about one or more services which gives WMI a
problem, but without logging a bug with Microsoft (if one can even
do that) I doubt much will change in that area in a hurry.

I'm quite happy to go another round or two if it helps. I
think that all we're likely to achieve is, as I say, a more
robust solution; we're unlikely to get the missing service
if the core WMI can't see it for some reason. In that sense,
it looks like you're better off using the win32service code.

Updated code below.
TJG

<code>

try:
  set
except NameError:
  from sets import Set as set

##
## Taken from a post to c.l.py by William J. Rice
##
import win32service
import win32con

accessSCM = win32con.GENERIC_READ
hscm = win32service.OpenSCManager (None, None, accessSCM)

win32_names = set ()

for service_info in win32service.EnumServicesStatus(hscm):
  win32_names.add (service_info[1])

import win32com.client

c = win32com.client.GetObject (
  "winmgmts:{impersonationLevel=Impersonate,authenticationLevel=Default}/root/cimv2"
)

wmi_names = set ()
services = c.ExecQuery ("SELECT * FROM Win32_Service")
n_services = services.Count
for n_service in range (n_services):
  try:
    service = services[n_service]
  except:
    print "Could not get service", n_service
  else:
    short_name = service.Properties_ ("Caption").Value
    wmi_names.add (short_name)
  
print "In win32:"
print ", ".join (win32_names)
print

print "In wmi:"
print ", ".join (wmi_names)
print

print "only in win32:", ", ".join (win32_names - wmi_names)
print

print "only in wmi:", ", ".join (wmi_names - win32_names)
print

</code>

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list