Problem with - WMI()

Tim Golden tim.golden at viacom-outdoor.co.uk
Wed Sep 27 12:37:43 EDT 2006


[kath]

| the following is code snippet I used to run plink command from Python.
| If I run plink command directly it pops out a command prompt 
| window. To make the command promot window invisible I used WMI 
| as follows. 

I'm not sure WMI is necessarily the best way to do this. You
might want to look at win32process.CreateProcess. But to
address the particular issue...

| The command runs perfect. But it throughs the error 
| following the code snipet.

| def run_command():
|    cmd="plink -v -N -batch -l username-pw password-L
| 3307:127.0.0.1:3306      dbb.mydatabase.com"
|    c = wmi.WMI ()
|    startup = c.Win32_ProcessStartup.new (ShowWindow=win32con.SW_HIDE)
|    pid, result =
| c.Win32_Process.Create(CommandLine=cmd,ProcessStartupInformati
on=startup)

| D:\...\...\library.zip\wmi.py:215:

The fact that wmi.py is in library.zip suggests that you're
running from py2exe or something similar. Might have a bearing
on the situation...

| Unhandled exception in thread started by <function run_command at
| 0x00D05130>

"... exception in thread" is almost certainly the vital bit of
that traceback, along with "wmi.pyc ... in connect"

| Traceback (most recent call last):
|   File "newfma.py", line 159, in run_command
|   File "wmi.pyc", line 975, in connect
|   File "wmi.pyc", line 220, in handle_com_error
| wmi.x_wmi: 0x800401e4 - Invalid syntax

It's almost certainly -- despite the misleading "Invalid syntax"
message -- the fact that you're running in a thread (albeit
unwittingly) and need to call pythoncom.CoInitialize () somwhere
before the c = wmi.WMI () line. That's basically because under
the covers WMI is based on COM/DCOM which needs to be instructed
how to thread.

TJG

________________________________________________________________________
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