[python-win32] wrapping a IUnknown pointer into a Dispatch interface

Sébastien de Menten sdementen at gmail.com
Mon Dec 19 17:17:07 CET 2011


I am using a call to a windows function via ctypes to retrieve a pointer to
the IDispatch interface of an Excel application, something like:

oledll.oleacc.AccessibleObjectFromWindow(hnd, OBJID_NATIVEOM,
byref(automation.IDispatch._iid_), byref(pointer_idispatch))

where hnd is the window handle to the proper excel window.

In the variable result (with type POINTER(IUnknown)), I have the pointer to
the interface.
Currently, to be able to call methods/properties on this COM object, I call
the "wrap" function from the comtypes.client library and then use the COM
object, something like:

app = wrap( pointer_idispatch )
print app.Application.Name '==> "Microsoft Excel"
app.Application.ActiveWorkbook.ActiveSheet.Cells(1,1).Value[()] = 1

However, this is slow (a coupe of seconds to do the wrap) and the resulting
object is less friendly to use than the COM object I can get with win32.
For instance, i need to use

app.Application.ActiveWorkbook.ActiveSheet.Cells(1,1).Value[()] = 1

instead of

app.Application.ActiveWorkbook.ActiveSheet.Cells(1,1).Value = 1

So my question, how could I instantiate a COM IDispatch object from win32
(equivalent of 'app' hereabove) when I have the pointer to the IDispatch
interface ('pointer_idispatch' hereabove) ?

thank you

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20111219/4e6a2832/attachment-0001.html>

More information about the python-win32 mailing list