How do I access COM object's nondefault interface?

Carl Waldbieser waldbie at attglobal.net
Sun Aug 18 03:29:00 EDT 2002


This technique also has a subtle problem that it doesn't always work-- here
is when and why:

As long as the COM method invocations don't cross apartments, everything
will work fine.  However, when cross-apartment calls are made, the internal
COM library tries to increase throughput by caching info in an entity known
as the "proxy manager".  One of the things the proxy manager happens to do
is cache the *first* IDispatch pointer that has been marshalled from the
object.  That means if the default interface was already passed to the proxy
manager, that is the IDispatch interface it is going to hand out, no matter
what interface x.m in the example below tries to return.

I am also interested in any information concerning how to access COM custom
interfaces from Python.  I've read "Python Programming on Win32", but I
didn't see any examples of how to communicate through an arbitrary interface
using pythoncom (though the chapter on advanced COM seemed to hint that this
was possible).  I would appreciate any URLs to aditional literature on the
subject as my Google searches pretty much tell me that "COM" is a pretty
common world on the Web.

Carl Waldbieser
waldbie at attglobal.net

"Markus Willi" <nallanews at bluewin.ch> wrote in message
news:3D5EB961.3050705 at bluewin.ch...
> Josef Sachs wrote:
> > Can anyone tell me how to access a COM object's nondefault interface?
>
> I had the same question and after searching the net and watching source
> (win32com/...) i come to this hack:
>
> given:
>    class x with default interface X
>    class y with default interface Y
>    class y implements the interface Z too
>    class x has a member (m) wich return a object y through the interface Z
>
> solution:
>    # y has now the Z interface
>    y = x.m
>    # y has now the 677...5A9 interface - Y interface
>    y = win32com.client.Dispatch(y, 'm',
> '{677B5E26-A514-11D6-8F69-0048545505A9}', UnicodeToString=0)
>
>
> I don't know if this hack is usefull for you. But I don't like it,
because:
> - I need the tool makepy
> - the second parameter could be anything, the eg. I found used the name
> of a member function from the class x?!?
> - the third parameter should be a name of the form "library.class",
> instead the CLSID
>
> If I use the tool makepy to generate python code for early binding, I
> can not belive why I have to type somthing like this
> x = win32com.client.Dispatch("library.class")
> instead
> x = library.class.
> But this is stuff for a new thread.
>
> OK. my english experience are better than python ;) That mean I use
> python just four days now and its great. I hope someone has a better
> solution to change the interface.
>
> Markus
>





More information about the Python-list mailing list