Tip: Windows internals using wmi

Tim Golden tim.golden at viacom-outdoor.co.uk
Tue Oct 7 04:18:04 EDT 2003


"Colin Brown" <cbrown at metservice.com> wrote in message news:<3f808185$1 at news.iconz.co.nz>...
> Following down these links gets to:
> 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/ht
> ml/scripting06112002.asp
> 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/ht
> ml/scripting08132002.asp
> 
> These are well worth a look if you want to know what wmi is all about. Table
> 1 indicates that you use the "Registry provider"  wmi interface for
> modifying registry settings. I do not know if the python wmi interface
> supports this.

May I also recommend:

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/scrguide/sas_reg_fzit.asp

which gives something of a worked example. 

As to whether the Python wmi interface will handle 
this, it depends... The wmi module is a fairly 
lightweight wrapper around Mark Hammond's win32com 
module, proxying a few GetObject and attribute calls, 
so following the example above, this should be possible:

import wmi
c = wmi.WMI (moniker="winmgmts://localhost/root/default")
for i in c.StdRegProv ():
  reg = i

reg.EnumValues etc. etc.

However... when I try this on my Win2k machine 
I run into two problems:

1) The root\default namespace (which the registry 
provider uses) does not have the SubclassesOf method 
which the module uses to determine available classes 
and give you the easy attribute access.

2) Even when you use the wmi object's instances 
method to query for the StdRegProv directly, it 
doesn't complain (as it does if, for example, 
you ask it for foobar) but it doesn't return 
any instances either.

[NB For the purposes of this example 
I've patched the WMI __init__ method 
to ignore SubclassesOf]

>>> c = wmi.WMI (moniker="winmgmts://localhost/root/default")
>>> print c.instances ("StdRegProv")
[]
>>> print c.instances ("foobar")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "o:\python-site-packages\wmi.py", line 466, in instances
    handle_com_error (error_info)
  File "o:\python-site-packages\wmi.py", line 111, in handle_com_error
    raise x_wmi, "\n".join (exception_string)
wmi.x_wmi: 0x80041010 - OLE error 0x80041010
>>>

Just for the moment this has me flummoxed. 
I suspect there's some extra bit of compiling 
or setting up that has to be done at the WMI end, 
but at present I don't have the time (or the 
need for myself) to look. If anyone has any luck, 
please let me know and if needs be I'll patch the 
module to cope.

Once the wmi object wraps the StdRegProv, everything
else should fall out nicely (but see my earlier 
remarks concerning remote security).

TJG




More information about the Python-list mailing list