从VB.NET到COM对象的封送数组

时间:2009-05-25 08:49:22

标签: vb.net interop marshalling

我有一个调用COM方法的VB6程序,传递2个数组作为参数,并期望在响应中填充2个数组。

代码是这样的,其中ItemIDs和ItemClientHandles是输入数组参数,而MyItemServerHandles和Errors由COM对象填充。

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)

现在我想从VB.NET调用相同的方法,并尝试这样的方法来定义数组:

<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

但无法让它工作,因为我收到错误(我试图从意大利语翻译):

  

无法从类型转换对象   输入'System.Int32 [*]'   'System.Int32 []'

将数组从VB.NET托管代码编组到非托管COM代码的正确方法是什么?

由于

1 个答案:

答案 0 :(得分:1)

解决。

输出数组必须简单地声明为

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

当从调用返回时,它们将作为Int32数组正确编组。