COM interfaces and inheritance

Alan Kennedy alanmk at hotmail.com
Tue Nov 19 07:43:09 EST 2002


Rob Sykes wrote:

> I'm having a problem with a (3rd party) IDispatch interface

> I need to create a new bar object but the 3rd party interface gives
> me a method which returns foo objects

Thw win32 extensions provide explicitly for this situation, i.e. where
the returned COM object implements several different interfaces, but
functions return the interface that you don't want.

First, you've got to run the win32 utility script

Lib/site-packages/win32com/client/makepy.py

on the library you're using.

This will create a pure python module that interfaces to your COM
library. These generated python modules are stored in the directory

Lib/site-packages/win32com/gen_py

Inside this generated file appears all of the code that you need to turn
an object implementing one interface into an object implementing another
interface (assuming, of course, that it does actually implement that
interface).

The first step is to get a handle on the module for your COM library,
like so

from win32com.client.gencache	import GetModuleForProgID
myModule = GetModuleForProgID('your.com.library.name.here')

Each possible interface in that module will have a class to represent
it. Each of the "constructors" for those classes will recognise if you
have passed it an instance of an object that implements a related
interface. If  you have, then it will return to you an instance that
implements the interface you need.

So some code might look like this

objA = createObjA() # Returned object implements interface A
objB = myModule.IInterface2(objA)
# Bingo!

At least, that works for me :-)

I hope this helps, and that I've explained it clearly enough.

regards,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan



More information about the Python-list mailing list