[python-win32] VB Interfaces

Blair Hall b.hall@irl.cri.nz
Fri, 13 Dec 2002 12:02:00 +1300


Well I'm afraid that my first posting contained a mistake. I've been able
to get a little closer to the problem that is occuring in my actual VB DLL 
code.

Here, then is an update on my problem.

The following VB classes are compiled into a DLL

' ...... INumber.cls (an Interface class)
Function setn(ByVal i As Integer)
	'n = i
End Function

Function getn() As Integer
	'getn = n
End Function

'..........Number.cls (a concrete class)
Implements INumber
Private n As Integer

Function INumber_setn(ByVal i As Integer)
	n = i
End Function

Function INumber_getn() As Integer
	INumber_getn = n
End Function

'................. test.cls
' A factory for making Number objects
Function makeN(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

' A way of displaying the value of an arbitrary number of INumber objects
Function val(ParamArray n())
On Error GoTo err
	Dim inf As INumber
	Dim i As Integer
	For i = LBound(n) To UBound(n)
		' Set inf = n(i)
		' MsgBox "1. Value = " & inf.getn
		MsgBox "2. Value = " & n(i).getn
	Next
	Exit Function
err:
	MsgBox "error" & err.Description
End Function

Now this code can be called from a VB script, e.g.:

' .......... main.bas
Sub Main()
	Dim t As New test
	Dim n As INumber
	Set n = t.makeN(5)
	t.val n, n

However, from Python it fails:

 >>> t = Dispatch("TestDll16.test")
 >>> n = t.makeN( 23 )
 >>> t.val( n,n )

The VB error MsgBox says that the object does not support the method requested.

Now, if one exchanges the two commented lines inside the For loop of the val
function above with the single line (MsgBox "2. Value = " & n(i).getn) then the
Python code does work (and so does the original VB script).

This seems to be the crux of my problem. In the VB DLL I am trying to
use from Python, there are a number of such VB 'short cuts'.

Why is it that when VB is calling this code it works, but a Python
client makes the VB code fail?

It seems that a rewrite of the VB code will fix this, but I am reluctant to
do that unless I understand the failure mechanism better and understand
precisely what should be changed.

Thanks for helping.