py2exe for WMI

Thomas Heller theller at python.net
Tue May 24 13:53:40 EDT 2005


Tim Golden schrieb:
> [Thomas Heller]
> 
> [... snip problems with py2exe & WMI ...]
> 
> | The OP of the thread Tim mentions here already had a solution, if I 
> | understood him correctly, and the other things that were suggested 
> | didn't work.  Basically, IIUC, he did add the typelib 
> | wrappers from the 
> | 'other' windows version manually to the library.zip file.
> | 
> | Totally different solutions would be:
> | - change Tim's wmi.py module so that it works with dynamic 
> | binding (no 
> | typelib wrappers needed)
> | or
> 
> Well, if it helps, there's a version of the WMI module
> that uses dynamic binding on my site:
> 
> http://timgolden.me.uk/python/downloads/wmi-0.6a.py
> 
> I've hardcoded the relevant constants (which is pretty 
> much all I was using early binding for) and replaced 
> any other EnsureDispatch calls with simple Dispatch.

It may be dangerous since the constants may or may not be the same on 
different versions, at least in theory.  I've appended a small module 
which allows to bind constants from the type library at runtime.
Not much error checking, but you may get the idea.  I'll leave providing 
a proper patch for pywin32, if it makes sense, to those that have more 
time ;-)

> Does anyone want to give it a try to let me know if
> it helps? I don't use py2exe myself.

The simple test case I have works on XP (I don't use wmi myself, except 
as sample for py2exe).

Thomas

# constants.py

class ProvideConstants(object):
     """A class which, when called on a win32com.client.Dispatch object,
     provides lazy access to constants defined in the typelib.

     They can be accessed as attributes of the _constants property."""
     def __init__(self, comobj):
         comobj.__dict__["_constants"] = self
         # Get the typelibrary's typecomp interface
         self.__typecomp = \
comobj._oleobj_.GetTypeInfo().GetContainingTypeLib()[0].GetTypeComp()

     def __getattr__(self, name):
         if name.startswith("__") and name.endswith("__"):
             raise AttributeError, name
         result = self.__typecomp.Bind(name)
         # Bind returns a 2-tuple, first item is TYPEKIND,
         # the second item has the value
         if not result[0]:
             raise AttributeError, name
         return result[1].value

if __name__ == "__main__":
     from win32com.client import Dispatch
     d = Dispatch("WbemScripting.SWbemLocator")
     ProvideConstants(d)

     print d._constants.wbemImpersonationLevelAnonymous
     print d._constants.wbemImpersonationLevelDelegate



More information about the Python-list mailing list