[python-win32] "Casting" COM objects

Jens B. Jorgensen jens.jorgensen@tallan.com
Wed, 02 Apr 2003 14:39:46 -0600

Based on the fact that makepy does its work by looking through type 
libraries it would be possible for it to generate the type traversal 
mechanisms you speak of, but unfortunately (or fortunately?) it doesn't 
as far as I know. The wrapper classes that are generated seem to be more 
geared towards representing interfaces (thank goodness) rather than the 
VB way of typing objects according to class and then making the 
interface the same as the default interface. It would certainly be 
possible for makepy to generated wrapper classes that could remember 
what classID they came from and in that case be able to safely support 
type-traversal methods such as you suggest but this doesn't seem to be 
the case. "casting" the type by passing the python object reference you 
have to the constructor of the object wrapping the interface you want is 
the method that I use.

Paul Prescod wrote:

> Consider the following case:
> class A:
>     def foo(self): pass
> class B(A):
>     def bar(self): pass
> class C(A):
>     def car(self): pass
> Now pretend Python had static typing:
> class Z:
>     def __init__(self, thing):
>     self.thing = thing
>     def get_A() returns A:
>     return self.thing
> Now in statically typed languages like C or IDL, it is perfectly legal 
> for C to return a "B" because a B isa A. Then, client code casts from 
> an A reference to a B reference to get access to the bar() function.
> Now imagine the code above is the output of MakePy. The object 
> returned by MakePy is a B wrapped in a proxy for an A. What I would 
> like to know is whether there is a function out there that can take 
> the _oleobj_ pointer and determine at runtime what the best proxy is 
> for it. I've figured out how to do some casting "manually" by 
> importing a gen_py'd module and selecting the right class, but I 
> wonder if there is an easier way
>  Paul Prescod
> _______________________________________________
> Python-win32 mailing list
> Python-win32@python.org
> http://mail.python.org/mailman/listinfo/python-win32

Jens B. Jorgensen

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"