COM Question - QueryInterface?

Toby Dickenson htrd90 at
Thu Jun 10 00:00:07 CEST 1999

Chris Tavares <tavares at> wrote:

>I've got a (hopefully easy) question about python's COM support.
>I'm developing a COM object in C++ which implements the COM collection
>idiom - an Item method that returns another object. The code in Python
>I'm using is something like this:
>from win32com.client import Dispatch
>drives = Dispatch("AmAccess.Drives")
>drive = drives.Item("c:")
>dir = drive.Item("some-area")
>On this last line, I get an AttributeError for Item.
>I think I know the reason why - the interface returned from Drives.Item
>isn't a dual, it's an oleautomation compatible vtable interface.
>IDispatch on that object is implemented separately. So, PythonCOM sees
>the VT_UNKNOWN return type and wraps a PYIUnknown object around it. I
>need to be able to do a QueryInterface for IDispatch.

Your analysis sounds about right, this should give you the IDispatch interface
returned by querying for IID_IDispatch:

drive = drives.Item("c:")._oleobj_.QueryInterface(pythoncom.IID_IDispatch)

A better approach might be to make the interface dual? It sounds like you have
already tackled the painful bits of making your interface dual; implementing
IDispatch (under IID_IDispatch) and sticking to the oleautomation compatible
restrictions. Making it dual should be an easy step, and will avoid this and
several different non-python problems too.

Toby Dickenson

More information about the Python-list mailing list