Returning Multiple Results (UDT?) From An Interop Call - VB6 <--> VB.NET

冷暖自知 提交于 2019-12-04 19:24:33
Yatrix

Have you considered passing your "stuff" as a dataset or using XML Serialization? I'm not real familiar with VB6, but I would think this would work. In order to pass a custom something between two of anything, they both have to be "aware" of that something.

You could also create a wrapper around the interop so that you can add a reference to your stuff to avoid code duplication. I can't say for sure, but I don't know of a way to just make your stuff passable between the two.

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

I used REGASM /tlb to create a type library for this DLL.

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.

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
}

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!