Problem listing services with wmi

  • Thread starter =?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=
  • Start date
?

=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=

Hello,

I'm pretty new to Python, though I have a fair bit of experience with
C/C++, Java, Perl, PHP and others.

I installed Tim Golden's wmi module
(http://tgolden.sc.sabren.com/python/wmi.html), in the hopes it would
help me list and work with services on my Win32 machine. Now, everything
seems fine except for one thing : Listing services!

I tried running a couple of the examples on Tim's more examples page in
the python shell (for example, List all running processes, Show the
percentage free space for each fixed disk, Show the IP and MAC addresses
for IP-enabled network interfaces, etc.) and they worked fine. But when
I try to run the first example which is on the first page above, I get
an error. The code is :

import wmi

c = wmi.WMI ()
for s in c.Win32_Service ():
if s.State == 'Stopped':
print s.Caption, s.State


and I get :

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "G:\Python-2.4\Lib\site-packages\wmi.py", line 404, in __call__
return self.wmi.query (wql)
File "G:\Python-2.4\Lib\site-packages\wmi.py", line 583, in query
raise WMI_EXCEPTIONS.get (hresult, x_wmi (hresult))
wmi.x_wmi: -2147217398

(the exception seems to be thrown on the "for s in..." line)
I have only found one discussion in this newsgroup's archives that seems
to talk about this problem
(http://groups-beta.google.com/group...3483?q=WMI_EXCEPTIONS&rnum=1#0364b7cd22d73483)
and the fix they suggest there (calling pythoncom.CoInitialize () before
instantiating the WMI object) doesn't seem to work in this case. In
other words, this code :

import wmi
import pythoncom

pythoncom.CoInitialize ()
c = wmi.WMI ()
for s in c.Win32_Service ():
if s.State == 'Stopped':
print s.Caption, s.State

gives me the same result as above.

Could someone please point me in the right direction to find out what's
wrong?

Thanks in advance,

J-S
 
L

Larry Bates

Might not be the problem but try without the leading
spaces before your method calls.

for s in c.Win32_Service ():

should be

for s in c.Win32_Service():

-Larry
 
L

Larry Bates

Might not be the problem but try without the leading
spaces before your method calls.

for s in c.Win32_Service ():

should be

for s in c.Win32_Service():

-Larry
 
?

=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=

Hello Larry,
Might not be the problem but try without the leading
spaces before your method calls.

for s in c.Win32_Service ():

should be

for s in c.Win32_Service():

No change. For reference, the reason I know it's the c.Win32_Service()
call that's throwing the exception is that if I try to run this :

import wmi
import pythoncom

pythoncom.CoInitialize()
c = wmi.WMI()
services = c.Win32_Service()
for s in services :
print s.Caption, s.State

the exception gets thrown on the "services = ..." line.

Out of curiosity, is there any reason why whitespace between the method
name and parameter list parentheses isn't good? Because the code I
posted before was copy-pasted from Tim Golden's site
(http://tgolden.sc.sabren.com/python/wmi.html) and the spaces were there.

Thanks,

J-S
 
L

Larry Bates

I guess I was wrong and it does work, but from Python Style Guide:

Whitespace in Expressions and Statements
Pet Peeves

Guido hates whitespace in the following places:

<snip>
- Immediately before the open parenthesis that starts the argument
list of a function call, as in "spam (1)". Always write
this as "spam(1)".

<end snip>

-Larry
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top