从互操作调用返回多个结果(UDT?)-VB6<-> VB.NET [英] Returning Multiple Results (UDT?) From An Interop Call - VB6 <--> VB.NET
问题描述
我希望Interop用户控件(VB.NET)返回并接受来自get / set调用的多个值。我假定用户定义类型(UDT)是正确的方法,但是我不断从VB6编译中获得变量使用Visual Basic不支持的自动化类型。如何在互操作控件和VB6应用程序之间传递多个值?
I’d like an Interop user control (VB.NET) to return and to accept multiple values from a get/set call. I assumed a user defined type (UDT), would be the correct way but I keep getting a "Variable uses an Automation type not supported in Visual Basic" from the VB6 compile. How is passing multiple values between an interop control and the VB6 application done?
VB.NET(互操作)代码,即带有.NET ListView的控件
VB.NET (Interop) code, a control with a .NET ListView
Structure Employee
Dim Firstname As String
Dim Lastname As String
End Structure
...
Public Property MyReadListViewData() As Employee
Get
Dim myEmployee As Employee
myEmployee.Lastname = ListView1.SelectedItems(0).Text
Return myEmployee
End Get
Set(ByVal value As Employee)
Me.ListView1.SelectedItems(0).Text = value.Lastname
End Set
End Property
典型的VB6代码:
Private Sub Command4_Click()
Dim myEmployee As Employee
myEmployee = MyToolStrip1.MyReadListViewData
Text3.Text = myEmployee.Lastname
End Sub
VB6中出现变量使用Visual Basic不支持的自动化类型错误的原因是,在类型库中创建的记录与VB6不兼容。我创建了一个新的VB2005项目,并将COM Visible设置为true:
The reason why you are getting the 'Variable uses an Automation type not supported in Visual Basic' error in VB6 is because the record created in the type library is not compliant with VB6. I created a new VB2005 project with COM Visible set to true:
Public Class Boing
Public Structure Employee
Dim FirstName As String
Dim LastName As String
End Structure
Public Sub GetEmployee(ByRef e As Employee)
e.FirstName = "Mark"
e.LastName = "Bertenshaw"
End Sub
End Class
我使用REGASM / tlb为该DLL创建类型库。
I used REGASM /tlb to create a type library for this DLL.
我还创建了一个测试VB6项目:
I also created a test VB6 project:
Private Sub Command_Click()
Dim udtEmployee As TestDotNetStructures.Employee
Dim oBoing As TestDotNetStructures.Boing
oBoing.GetEmployee udtEmployee
End Sub
我成功地重现了您的错误。
I successfully repro'd your error.
然后,我查看了REGASM生成的类型库(使用PowerVB类型库编辑器)。事实证明,为Employee类型创建的RECORD看起来像这样:
I then looked at the type library generated by REGASM (using the PowerVB Type Lib Editor). It turns out that the RECORD created for the Employee type looked like this:
Record Employee
{
LPWSTR FirstName
LPWSTR LastName
}
包含LPWSTR的记录无效COM方法中的参数。 LPWSTR不是COM兼容类型。另一方面,BSTR肯定是。
A record containing LPWSTR is not valid as an argument in a COM method. LPWSTR is not a COM-compliant type. On the other hand, BSTR definitely is.
解决方法是在您的VB.NET代码中添加编组属性,以告诉VB.NET将字符串作为BSTR传递:
The fix is to add marshalling attributes to your VB.NET code to tell VB.NET to pass strings as BSTR:
Imports System.Runtime.InteropServices
<Microsoft.VisualBasic.ComClass()> Public Class Boing
Public Structure Employee
<MarshalAs(UnmanagedType.BStr)> Dim FirstName As String
<MarshalAs(UnmanagedType.BStr)> Dim LastName As String
End Structure
Public Sub GetEmployee(ByRef e As Employee)
e.FirstName = "Mark"
e.LastName = "Bertenshaw"
End Sub
End Class
这篇关于从互操作调用返回多个结果(UDT?)-VB6<-> VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!