[python-win32] How to find corresponding WMI/COM object for given Windows Registry key?

Radek Holý radekholypublic at gmail.com
Sat Jun 23 15:03:01 CEST 2012


2012/6/18 Radek Holý <radekholypublic at gmail.com>:
> 2012/6/18 Tim Golden <mail at timgolden.me.uk>:
>> On 18/06/2012 00:58, Radek Holý wrote:
>>>
>>> My question is probably poorly formulated.
>>> In fact -- as I discovered -- some WMI objects reflect their values in
>>> the Windows Registry keys (for example there is mapping
>>> “root\cimv2:Win32_OSRecoveryConfiguration.AutoReboot” in
>>>
>>> “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CrashControl\AutoReboot”).
>>> Is there some *common* attribute of WMI/COM/OLE objects giving the key
>>> path?
>>
>>
>> I don't believe so. The thing about WMI is that it provides a
>> uniform and accessible layer on top of quite a lot of lower-level
>> APIs. In some cases, that's a layer over registry entries; in other
>> cases it's a layer over an actual API call. It may even be the only
>> publicly-accessible means of achieving some result.
>>
>> As it happens, for *methods* there is sometimes a clue as to the
>> underlying API. I have exposed this as (for want of a better name) the
>> provenance attribute of a method class. So if you do this:
>>
>> <code>
>> import wmi
>>
>> print wmi.WMI().Win32_Process.Create.provenance
>>
>> </code>
>>
>> You'll see something like this:
>>
>> Win32API|Process and Thread Functions|CreateProcess
>>
>> I'm not aware of any such thing for attributes, although it wouldn't
>> surprise me utterly if there were.
>
> I know that the dependence WMI <-> WinReg is not frequent. Saying
> “common attribute” I thought common to this (reflecting) type of
> objects. So it seems that it was an exception that I managed to print
> this thing.
> Or is it possible that the attribute belongs to underlaying COM or OLE
> object? (In the pywin32 implementation!)
>
> I wonder which object it was... :-/ What a pity that interactive shell
> does not remember the history of commands after rebooting the
> computer… :-D
> --
> Radek Holý
> Czech republic


Hello,

I was advised to look at the ``MappingStrings`` qualifier. So far it
seems that the only way is to list ``MappingStrings`` qualifiers of
all properties.
Something like this:

>>> import wmi
>>> import pywintypes
>>>
>>> def walk_namespace(namespace_path):
...     namespace = wmi.WMI(computer=".", namespace=namespace_path,
find_classes=True)
...
...     subclasses_names = namespace.classes
...     for subclass_name in subclasses_names:
...         subclass = getattr(namespace, subclass_name)
...
...         subclass_path = str(subclass.path())
...         subclass_mapping = get_mapping(subclass)
...         save_mapping(subclass_path, subclass_mapping)
...
...         for property in subclass.Properties_:
...             property_path = ".".join((subclass_path, property.Name))
...             property_mapping = get_mapping(property)
...             save_mapping(property_path, property_mapping)
...
...     subnamespaces = namespace.__Namespace()
...     for subnamespace in subnamespaces:
...         subnamespace_path = "/".join((namespace_path, subnamespace.Name))
...         walk_namespace(subnamespace_path)
...
>>> def get_mapping(object_):
...     try:
...         mapping = object_.Qualifiers_("MappingStrings")
...     except pywintypes.com_error as err:
...         if err.excepinfo[5] == -2147217406:
...             return ()
...         else:
...             raise
...     else:
...         return mapping.Value
...
>>> def save_mapping(path, mapping):
...     if mapping:
...         print(path, mapping)
...
>>> walk_namespace("root")

I hope that this code passes through all the classes and their
properties. And in this case, I hope that this code prints all
available "dependencies".

-- 
Radek Holý
Czech republic


More information about the python-win32 mailing list