[python-win32] VB Interfaces

Blair Hall b.hall@irl.cri.nz
Thu, 12 Dec 2002 16:23:20 +1300


This is really my first foray into the world of Python and COM
so I may be asking a dumb question. Here goes.

I have been trying to write client code for a
collection of small automation objects written in VB (by me).

The VB code uses an interface to support polymorphic behaviour
from a number of different classes. Unfortunately, Python
is not handling this interface the way I expected it to.

The following is not the actual code, but it shows
where my thinking about this comes unstuck:

 >>>>>>>>>> VB classes (complied into a dll):

' Interface class ...... INumber .cls
Function setn(ByVal i As Integer)
	' Set the numerical value
End Function

Function getn() As Integer
	' Return the numerical value
End Function

' Concrete number class........ Number.cls
Implements INumber
Dim n As Integer
Function INumber_setn(ByVal i As Integer)
	n = i
End Function

Function INumber_getn() As Integer
	getn = n
End Function

  ' Class with a factory function ......... test.cls
Function makeNumber(ByVal i As Integer) As INumber
	Dim n As INumber
	Set n = New Number
	n.setn i
	Set makeN = n
	Set n = Nothing
End Function

 >>>>>>>>>>

Now if I run makepy on this automation object, I can use these objects from 
Python.
For example,

 >>> t = Dispatch("MyDllName.test")
 >>> n = t.makeNumber( 4 )
 >>> n.getn()
 >>> 0

which is not the answer expected!
If I re-write makeNumber to use only Number objects
the Python code above works.

Now, I may be wrong, but  it seems that an
INumber object can be created without
being a reference to a Number object.
e.g.:

 >>> i = Dispatch("MyDllName.INumber")
 >>> i.setn(3)
 >>> i.getn()
 >>> 0

So I presume that when makeNumber is called
it creates an INumber object in Python that is not being
correctly initialised.

How should I proceed, IN GENERAL, to achieve the polymorphic
behaviour in Python that the VB code is designed to support?