[python-win32] Dynamic method creation and WMI
Tim Golden
tim.golden at viacom-outdoor.co.uk
Wed Dec 24 04:44:43 EST 2003
>From: Christian Wyglendowski
[snip need to autocomplete WMI attributes]
>class WMIObject:
> def __init__(self, object, query, computer='.'):
> wmi = GetObject('winmgmts://%s' % computer)
> self._wmiobj = wmi.ExecQuery('select * from %s where %s' %
>(object, query))[0]
> for meth in self._wmiobj.Methods_:
> newmethod = str(meth.Name)
> self.__dict__[newmethod] = lambda:
>self._wmiobj.ExecMethod_(meth.Name)
>
>Here is some output from a session in the interactive interpreter.
>
>>>> x = WMITools.WMIObject('Win32_Process', 'Name="TIWin.exe"')
>>>> x.GetOwner()
[snip traceback]
Well, this is no consolation, but it works for me (I cut-and-pasted your
code into WMIUtils.py and adjusted the line-endings as seemed appropriate):
C:\temp>python
ActivePython 2.2.3 Build 227 (ActiveState Corp.) based on
Python 2.2.3 (#42, Nov 13 2003, 09:57:55) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import WMIUtils
>>> x = WMIUtils.WMIObject ("Win32_Process", 'Name="notepad.exe"')
>>> x.GetOwner ()
<win32com.gen_py.Microsoft WMI Scripting V1.1 Library.ISWbemObject instance
at 0x13233264>
>>>
I'm running on Win2k with the builds you see above. I can't see any
particular reason
why it shouldn't work. As you probably discovered from googling, the error
code
is gives out (-2147217407 = 0x80041001) is so bland as to be useless.
As an aside, while this technique is fine for generating dictionary-level
attributes
which Pythonwin etc. can pick up, it does (obviously) involve the overhead
of
creating all the attributes every time you create a WMI object, and will
start to
get wobbly when you need to pass parameters into the methods called. By
this time,
you've probably seen that for yourself / got a more robust solution in
place, but
having gone through all the pain once for myself I thought I might at least
warn you. Since I don't use Pythonwin, I simply proxy the attribute calls
through
__getattr__ on demand.
TJG
________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. 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-win32
mailing list