COM Question - QueryInterface?

Chris Tavares tavares at connix.com
Thu Jun 10 01:56:25 CEST 1999


Toby Dickenson wrote:

> Chris Tavares <tavares at connix.com> 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

Toby,

Thanks. The reason that the interface in question isn't a dual is because my
object design depends quite heavily on having multiple interfaces. If you have
multiple duals, you end up with lots of problems with IDispatch, since you can't
safely implement more than one version of the same interface on a single object
(and lots of people have tried, REALLY HARD).

So what I'm doing is having multiple oleautomation compatible interfaces, and then
using a typelib-driven IDispatch implementation that I got from
http://www.sellsbrothers.com/tools/multidisp/index.html. This automagically merges
all my separate interfaces into a pseudo-object model. Really neat stuff, and
saves a TON of work.

Having tried that, I find that I now have access to IDispatch::Invoke. Well, what
I really wanted was win32com.client.dynamic.Dispatch. How do I get to the wrapped
IDispatch rather than the raw one?

-Chris






More information about the Python-list mailing list