Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
would create modules named
comInterfaces._EAB22AC0_30C1_11CF_A7EB_0000C05BAE0B_0_1_1
comInterfaces.SHDocVw
containing the Python wrapper code for the type library used by
Internet Explorer. The former module contains all the code, the
latter is a short stub loading the former.
"""
if isinstance(tlib, basestring):
# we accept filenames as well
tlib = comtypes.typeinfo.LoadTypeLibEx(tlib)
elif isinstance(tlib, (tuple, list)):
tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib[0]), *tlib[1:])
elif hasattr(tlib, "_reg_libid_"):
tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib._reg_libid_),
*tlib._reg_version_)
# determine the Python module name
fullname = _name_module(tlib)
# create and import the module
return _CreateWrapper(tlib, fullname)
tlib.GetDocumentation(-1)[:2])
self._register(name, typ, tlib)
return typ
ta = tinfo.GetTypeAttr()
tkind = ta.typekind
if tkind == typeinfo.TKIND_ENUM: # 0
return self.ParseEnum(tinfo, ta)
elif tkind == typeinfo.TKIND_RECORD: # 1
return self.ParseRecord(tinfo, ta)
elif tkind == typeinfo.TKIND_MODULE: # 2
return self.ParseModule(tinfo, ta)
elif tkind == typeinfo.TKIND_INTERFACE: # 3
return self.ParseInterface(tinfo, ta)
elif tkind == typeinfo.TKIND_DISPATCH: # 4
try:
# GetRefTypeOfImplType(-1) returns the custom portion
# of a dispinterface, if it is dual
href = tinfo.GetRefTypeOfImplType(-1)
except COMError:
# no dual interface
return self.ParseDispatch(tinfo, ta)
tinfo = tinfo.GetRefTypeInfo(href)
ta = tinfo.GetTypeAttr()
assert ta.typekind == typeinfo.TKIND_INTERFACE
return self.ParseInterface(tinfo, ta)
elif tkind == typeinfo.TKIND_COCLASS: # 5
return self.ParseCoClass(tinfo, ta)
elif tkind == typeinfo.TKIND_ALIAS: # 6
return self.ParseAlias(tinfo, ta)
elif tkind == typeinfo.TKIND_UNION: # 7
typ = typedesc.External(tlib,
name,
size,
align,
tlib.GetDocumentation(-1)[:2])
self._register(name, typ, tlib)
return typ
ta = tinfo.GetTypeAttr()
tkind = ta.typekind
if tkind == typeinfo.TKIND_ENUM: # 0
return self.ParseEnum(tinfo, ta)
elif tkind == typeinfo.TKIND_RECORD: # 1
return self.ParseRecord(tinfo, ta)
elif tkind == typeinfo.TKIND_MODULE: # 2
return self.ParseModule(tinfo, ta)
elif tkind == typeinfo.TKIND_INTERFACE: # 3
return self.ParseInterface(tinfo, ta)
elif tkind == typeinfo.TKIND_DISPATCH: # 4
try:
# GetRefTypeOfImplType(-1) returns the custom portion
# of a dispinterface, if it is dual
href = tinfo.GetRefTypeOfImplType(-1)
except COMError:
# no dual interface
return self.ParseDispatch(tinfo, ta)
tinfo = tinfo.GetRefTypeInfo(href)
ta = tinfo.GetTypeAttr()
assert ta.typekind == typeinfo.TKIND_INTERFACE
return self.ParseInterface(tinfo, ta)
elif tkind == typeinfo.TKIND_COCLASS: # 5
def ParseUnion(self, tinfo, ta):
union_name, doc, helpcntext, helpfile = tinfo.GetDocumentation(-1)
members = []
union = typedesc.Union(union_name,
align=ta.cbAlignment*8,
members=members,
bases=[],
size=ta.cbSizeInstance*8)
self._register(union_name, union)
for i in range(ta.cVars):
vd = tinfo.GetVarDesc(i)
name = tinfo.GetDocumentation(vd.memid)[0]
offset = vd._.oInst * 8
assert vd.varkind == typeinfo.VAR_PERINSTANCE
typ = self.make_type(vd.elemdescVar.tdesc, tinfo)
field = typedesc.Field(name,
typ,
None, # bits
offset)
members.append(field)
return union
# map TYPEFLAGS values to idl attributes
NAMES = {typeinfo.TYPEFLAG_FAPPOBJECT: "appobject",
# typeinfo.TYPEFLAG_FCANCREATE:
typeinfo.TYPEFLAG_FLICENSED: "licensed",
# typeinfo.TYPEFLAG_FPREDECLID:
typeinfo.TYPEFLAG_FHIDDEN: "hidden",
typeinfo.TYPEFLAG_FCONTROL: "control",
typeinfo.TYPEFLAG_FDUAL: "dual",
typeinfo.TYPEFLAG_FNONEXTENSIBLE: "nonextensible",
typeinfo.TYPEFLAG_FOLEAUTOMATION: "oleautomation",
typeinfo.TYPEFLAG_FRESTRICTED: "restricted",
typeinfo.TYPEFLAG_FAGGREGATABLE: "aggregatable",
# typeinfo.TYPEFLAG_FREPLACEABLE:
# typeinfo.TYPEFLAG_FDISPATCHABLE # computed, no flag for this
typeinfo.TYPEFLAG_FREVERSEBIND: "reversebind",
typeinfo.TYPEFLAG_FPROXY: "proxy",
}
NEGATIVE_NAMES = {}
return [NAMES[bit] for bit in NAMES if bit & flags] + \
[NEGATIVE_NAMES[bit] for bit in NEGATIVE_NAMES if not (bit & flags)]
Example:
GetModule("shdocvw.dll")
would create modules named
comInterfaces._EAB22AC0_30C1_11CF_A7EB_0000C05BAE0B_0_1_1
comInterfaces.SHDocVw
containing the Python wrapper code for the type library used by
Internet Explorer. The former module contains all the code, the
latter is a short stub loading the former.
"""
if isinstance(tlib, basestring):
# we accept filenames as well
tlib = comtypes.typeinfo.LoadTypeLibEx(tlib)
elif isinstance(tlib, (tuple, list)):
tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib[0]), *tlib[1:])
elif hasattr(tlib, "_reg_libid_"):
tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(tlib._reg_libid_),
*tlib._reg_version_)
# determine the Python module name
fullname = _name_module(tlib)
# create and import the module
return _CreateWrapper(tlib, fullname)
def GetBestInterface(punk):
"""Try to QueryInterface a COM pointer to the 'most useful'
interface.
Get type information for the provided object, either via
IDispatch.GetTypeInfo(), or via IProvideClassInfo.GetClassInfo().
Generate a wrapper module for the typelib, and QI for the
interface found.
"""
if not punk: # NULL COM pointer
return punk # or should we return None?
# find the typelib and the interface name
logger.debug("GetBestInterface(%s)", punk)
try:
try:
pci = punk.QueryInterface(comtypes.typeinfo.IProvideClassInfo)
logger.debug("Does implement IProvideClassInfo")
except comtypes.COMError:
# Some COM objects support IProvideClassInfo2, but not IProvideClassInfo.
# These objects are broken, but we support them anyway.
logger.debug("Does NOT implement IProvideClassInfo, trying IProvideClassInfo2")
pci = punk.QueryInterface(comtypes.typeinfo.IProvideClassInfo2)
logger.debug("Does implement IProvideClassInfo2")
tinfo = pci.GetClassInfo() # TypeInfo for the CoClass
# find the interface marked as default
ta = tinfo.GetTypeAttr()
for index in range(ta.cImplTypes):
if tinfo.GetImplTypeFlags(index) == 1:
break
else:
if ta.cImplTypes != 1:
# Hm, should we use dynamic now?
def FindOutgoingInterface(source):
"""XXX Describe the strategy that is used..."""
# If the COM object implements IProvideClassInfo2, it is easy to
# find the default autgoing interface.
try:
pci = source.QueryInterface(comtypes.typeinfo.IProvideClassInfo2)
guid = pci.GetGUID(1)
except comtypes.COMError:
pass
else:
# another try: block needed?
try:
interface = comtypes.com_interface_registry[str(guid)]
except KeyError:
tinfo = pci.GetClassInfo()
tlib, index = tinfo.GetContainingTypeLib()
from comtypes.client import GetModule
GetModule(tlib)
interface = comtypes.com_interface_registry[str(guid)]
logger.debug("%s using sinkinterface %s", source, interface)
return interface
def GetTypeInfo(self, index, lcid=0):
"""Return type information. Index 0 specifies typeinfo for IDispatch"""
import comtypes.typeinfo
result = self._GetTypeInfo(index, lcid)
return result.QueryInterface(comtypes.typeinfo.ITypeInfo)
def interface_type_flags(self, flags):
# map TYPEFLAGS values to idl attributes
NAMES = {typeinfo.TYPEFLAG_FAPPOBJECT: "appobject",
# typeinfo.TYPEFLAG_FCANCREATE:
typeinfo.TYPEFLAG_FLICENSED: "licensed",
# typeinfo.TYPEFLAG_FPREDECLID:
typeinfo.TYPEFLAG_FHIDDEN: "hidden",
typeinfo.TYPEFLAG_FCONTROL: "control",
typeinfo.TYPEFLAG_FDUAL: "dual",
typeinfo.TYPEFLAG_FNONEXTENSIBLE: "nonextensible",
typeinfo.TYPEFLAG_FOLEAUTOMATION: "oleautomation",
typeinfo.TYPEFLAG_FRESTRICTED: "restricted",
typeinfo.TYPEFLAG_FAGGREGATABLE: "aggregatable",
# typeinfo.TYPEFLAG_FREPLACEABLE:
# typeinfo.TYPEFLAG_FDISPATCHABLE # computed, no flag for this
typeinfo.TYPEFLAG_FREVERSEBIND: "reversebind",
typeinfo.TYPEFLAG_FPROXY: "proxy",
}
NEGATIVE_NAMES = {}
return [NAMES[bit] for bit in NAMES if bit & flags] + \
[NEGATIVE_NAMES[bit] for bit in NEGATIVE_NAMES if not (bit & flags)]