[Tutor] COM Interfaces

dominic.fox dominic.fox" <dominic.fox@ntlworld.com
Sun, 12 May 2002 22:52:16 +0100


I wonder whether anyone can help with a slightly more advanced Python/COM
question. I'm being impatient here - as helpfully suggested, I ought to go
and get Win32 Programming with Python, which no doubt contains the answer to
this question, but I've just spend thirty-odd quid on an admittedly
excellent Python book already this month and may have to hang on before I
can convince my spouse that I can afford another one. If there's a quick
answer someone can give me off the top of their head, then I'd be most
grateful for it.

I've managed to create an MSXML3 DOM and manipulate it in the ways I'm used
to from VB using win32all's Dispatch('MSXML2.DOMDocument'). This didn't work
for 'MSXML2.DOMDocument30' for some reason, which may have to do with my
main question  (the reason I'm using MSXML3, by the way, is that I'm a
little hazy about the current level of support in Python for XPath and
XSLT). This is good: it means that I can go into work tomorrow and talk
about how I might like to start using Python quite soon to do some of the
things we currently do in VB, and if they say "but what about XML / IE
Explorer automation / Attachmate 3270 SNA Client automation / etc." I can
say, "Oh, Python will do that too" with a reasonable amount of confidence.
That doesn't mean they'll let me use it, of course, but it might be a start.

A fairly common practice in writing VB code is to write a class which
"implements" the COM interface of another. This is about as good as it gets
in VB from an OO point of view; it means that you can create a number of
classes which share the same methods, properties and functions, and pass
instances of them interchangeably to client code. The trouble is that I
can't see how to access the implemented attributes of these dual (or
possibly multiple) interface classes using win32all's Dispatch. In VB you'd
say something like:

Dim GenericFoo as IFoo
Set GenericFoo = New ActualFoo1    ' ActualFoo1 "implements" IFoo, which has
a property Bar()
Msgbox GenericFoo.Bar()
Set GenericFoo = New ActualFoo2    ' ActualFoo2 also implements IFoo
Msgbox GenericFoo.Bar()

Now, assuming I had some VB classes IFoo, ActualFoo1 and ActualFoo2 which
actually did something useful, and I wanted to use them in Python. How would
I go about creating, and calling Bar() on, an instance of one of these
classes?

thanks for any help,
Dominic