views:

706

answers:

1

Hi,

I have a VB6 program which calls a COM method, passing 2 arrays as parameters and expecting 2 arrays to be populated in response.

The code is this, where ItemIDs and ItemClientHandles are the input array parameters and MyItemServerHandles and Errors are populated by the COM object.

Dim ItemIDs(2) As String
Dim ItemClientHandles(2) As Long
Dim Errors() As Long             ' Array for returned Item related errors
Dim MyItemServerHandles() As Long   ' Server Handles for Items

ItemIDs(1) = "2,VW1000,word"
ItemIDs(2) = "2,VW1002,word"

ItemClientHandles(1) = 1
ItemClientHandles(2) = 2

Call MyItems.AddItems(2, ItemIDs, ItemClientHandles, MyItemServerHandles, Errors)

Now I would like to call the same method from VB.NET, and tried something like this to define the arrays:

<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I4, SizeConst:=2)> Private MyItemServerHandles(2) As Int32 ' Server Handles for Items
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPWStr, SizeConst:=2)> Private ItemIDs(2) As String
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I4, SizeConst:=2)> Private ItemClientHandles(2) As Int32
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I4, SizeConst:=2)> Dim Errors(2) As Int32 ' Array for returned Item related errors

but can't get it to work, because I get an error (which I try to translate from italian):

Can't cast objects from type 'System.Int32[*]' to type 'System.Int32[]'

What is the correct way to marshal arrays from VB.NET managed code to unmanaged COM code?

Thanks

+1  A: 

Resolved.

The output arrays must be simply declared as

Private MyItemServerHandles As System.Array
Private Errors As System.Array

and they will be marshaled correctly as Int32 arrays when returning from the call.

Enrico Detoma