[python-win32] Cast CDispatch to DispatchBaseClass?

hauptmech hauptmech at gmail.com
Sat Sep 21 05:11:16 EDT 2019


I have a makepy generated class that returns an untyped object which needs
to be cast into one of a couple other makepy generated classes.

My code starts with:

import win32com.client as client
mod =
client.gencache.EnsureModule('{83A33D31-27C5-11CE-BFD4-00400513BB57}',0,SWAV,0)
swApp = client.GetActiveObject("SldWorks.Application")
model = swApp.ActiveDoc #Meant to be cast by the user to one of two
interfaces. Returns a win32com.client.CDispatch


CastTo (and the constructor for the generated class I need to cast to) fail
to set _oleobj_ correctly.

After poking around I got things to work with:

swModel = mod.IModelDoc2(model)
swModel.__dict__["_oleobj_"] =
 model._oleobj_.QueryInterface(mod.IModelDoc2.CLSID,
pythoncom.IID_IDispatch)


This feels like I'm doing something wrong. Is there a better way to do this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20190921/7172e8d2/attachment.html>


More information about the python-win32 mailing list