简体   繁体   English

使用互操作将对象数组从 VB6 返回到 C#

[英]Return array of objects from VB6 to C# using Interop

I need to return an array of initialized objects from VB6 into C# using interop.我需要使用互操作将一组初始化对象从 VB6 返回到 C# 中。 My VB function looks like我的 VB function 看起来像

Public Function CreateMyObjArray(ByVal MaxCount As Integer) As MyObj()

  Dim i As Integer
  Dim temparray() As MyObj
  ReDim temparray(MaxCount) As MyObj

  For i = 0 To MaxCount
      Set temparray(i) = New MyObj
  Next i

  CreateMyObjArray = temparray

End Function

Now, when I call this from C# after passing in the array by现在,当我在传入数组后从 C# 调用它时

Array InData = m_MyObjGenerator.CreateMyOjbArray(5);

I get a system.argumentexceptionerror where the message is我收到消息所在的 system.argumentexceptionerror

"Exception of type 'System.ArgumentException' was thrown.\r\nParameter name: typeName@0" "引发了 'System.ArgumentException' 类型的异常。\r\n参数名称: typeName@0"

I also get this error if my function has no parameters.如果我的 function 没有参数,我也会收到此错误。 The function works in VB from a form. function 在 VB 中从一个表单工作。 Likewise, the following function returns a MyObj just fine同样,下面的 function 返回一个 MyObj 就好了

Public Function CreateMyObj() As MyObj
 Set CreateMyObj = New MyObj
End Function

I know I can make a list of new MyObj's in the C# version and then.ToArray() it, but I would really like to get this working.我知道我可以在 C# 版本中列出新的 MyObj 列表,然后使用.ToArray() 它,但我真的很想让它工作。 Thanks.谢谢。

Solution Found out how to do it.解决方案找到了如何做到这一点。 I had to use tlbimp.exe without the /sysarray flag (which VS must use internally).我必须使用没有 /sysarray 标志的 tlbimp.exe(VS 必须在内部使用)。 After that, I was able to get everything working correctly.在那之后,我能够让一切正常工作。 Thanks for the help guys.谢谢你们的帮助。

I am sorry that I can't try out some code to really help you solve this.很抱歉,我无法尝试一些代码来真正帮助您解决这个问题。

Having said that, set InData to be an Object .话虽如此,将InData设置为Object

Object InData = m_MyObjGenerator.CreateMyOjbArray(5);

After that statement executes, use the watch window to determine the type of InData .该语句执行后,使用手表 window 来确定InData的类型。 Modify the code (change the type of InData from Object to the type that you discovered using the watch window).修改代码(将Object InData为您使用监视窗口发现的类型)。

Hope that helps.希望有帮助。

First off let's clean up that VB a bit:首先让我们清理一下VB:

Public Function CreateMyObjArray(ByVal MaxCount As Integer) As MyObj()     
  ''// MaxCount = 5 would allocate 6 array items with your old code
  ''// Also: do this in one line rather than with an expensive "ReDim"
  Dim temparray(MaxCount-1) As MyObj 

  Dim i As Integer
  For i = 0 To MaxCount -1 
      Set temparray(i) = New MyObj
  Next i

  CreateMyObjArray = temparray
End Function

Finally, you C# should look like this:最后,您的 C# 应该如下所示:

MyObj[] InData = m_MyObjGenerator.CreateMyObjArray(5);

Where MyObj is the marshalled type use when talking to your vb code. MyObj 是与您的 vb 代码交谈时使用的编组类型。 As another poster suggested, you can set it to Object and step to it to let Visual Studio tell you what type to use exactly.正如另一张海报所建议的那样,您可以将其设置为Object并逐步执行,让 Visual Studio 告诉您确切使用的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM