Repost of Question on ParamArray usage with Com Interop

T

Terry

Hi,
I am using VS2005 and am having a problem accessing a function in a .Net
assembly exposed to Com that uses a ParamArray.
Here is a contrived example...

Public Interface IAdder
Function Adder(ByVal ParamArray Ints() As Integer) As Integer
End Interface

<ComClass(Foo.ClassId, Foo.InterfaceId, Foo.EventsId)> _
Public Class Foo
Implements IAdder
#Region "COM GUIDs"

Public Sub New()
MyBase.New()
End Sub

Public Function Adder(ByVal ParamArray Ints() As Integer) As Integer
Implements IAdder.Adder
Dim temp As Integer = 0
For Each i As Integer In Ints
temp += i
Next
Return temp
End Function
End Class

And the VB6 code ...

Dim c As New ComClass.Foo
Dim a As Integer
a = c.Adder(1, 2, 3, 4) <=== gets the following runtime error:

Function or interface marked as restricted, or the function uses an
Automation type not supported in Visual Basic

I did a google search and came up with the following KB article (which seems
to only relate to VS 2002 and 2003...

http://support.microsoft.com/kb/327084

The article says the work around is to pass the ParamArray ByRef, but VS2005
says a ParamArray can NOT be passed ByRef!

So, what do I do?

TIA,
 
T

Thorsten Doerfler

Terry said:
I am using VS2005 and am having a problem accessing a function in a .Net
assembly exposed to Com that uses a ParamArray.
Here is a contrived example...

Public Interface IAdder
Function Adder(ByVal ParamArray Ints() As Integer) As Integer
End Interface

<ComClass(Foo.ClassId, Foo.InterfaceId, Foo.EventsId)> _
Public Class Foo
Implements IAdder
#Region "COM GUIDs"

Public Sub New()
MyBase.New()
End Sub

Public Function Adder(ByVal ParamArray Ints() As Integer) As Integer
Implements IAdder.Adder
Dim temp As Integer = 0
For Each i As Integer In Ints
temp += i
Next
Return temp
End Function
End Class

And the VB6 code ...

Dim c As New ComClass.Foo
Dim a As Integer
a = c.Adder(1, 2, 3, 4) <=== gets the following runtime error:

Function or interface marked as restricted, or the function uses an
Automation type not supported in Visual Basic

ParamArray in COM/VB6 is always Variant (Object). But this wouldn't
solve your problem, the ByVal/ByRef handling difference remains.
http://support.microsoft.com/kb/327084

The article says the work around is to pass the ParamArray ByRef, but VS2005
says a ParamArray can NOT be passed ByRef!

Maybe it was right for VS2002/2003.
So, what do I do?

Just an idea: Add a method to your COM-Class which expects a ByRef
Object-Array (COM Variant). In your VB6 project provide a wrapper
method wich takes the ParamArray. This wrapper passes the
Param/Variant-Array to your .NET COM-Class. The intermediate step over
the Variant-Array is necessary, because VB6 ParamArrays aren't genuine
arrays.

<ComClass()> _
Public Class ParamTest
Public Sub Foo(ByVal ParamArray params() As Object)
Me.FooCompat(params)
End Sub

Public Sub FooCompat(ByRef params() As Object)
For i As Integer = 0 To params.Length - 1
MsgBox(params(i).ToString)
Next
End Sub
End Class

' VB6:
Private Sub Foo(ParamArray params())
Dim lTest As ParamTest
Dim lTemp() As Variant
Set lTest = New ParamTest
lTemp = params
lTest.FooCompat lTemp
End Sub

Call Foo (1, 2, 3, 4)

Of course this way you lose type-safety.

Thorsten Doerfler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top