How to pass uninitialized array from VB6 to .NET via COM?

I want to replace a component written in VB6 with a new component written in VB.NET. The new component should work with other VB6 applications until they are replaced. I am having problems with a function that takes string arrays as parameters.

I am using the VB6 class that contains this function to replicate the problem:

Public Function MyMethod(arr() As String, arr2() As String, Result$) As Integer     
    Result = Join(arr, ", ")
    MyMethod = 0        
End Function

I can successfully call this from a test program (VB6), like this, which displays "Hello, World":

    Dim obj As Object
    Dim arr() As String
    Dim arr2() As String
    Dim result As String

    Set obj = CreateObject("MyHelloWorld.MyClass")
    'Set obj = CreateObject("HelloWorldCOMNet.MyNetClass")

    arr = Split("Hello World", " ")
    'ReDim arr2(0)
    result = ""

    If Not obj.MyMethod(arr, arr2, result) Then
        MsgBox result
    End If

I cannot change the actual VB6 application, but I want to replace the VB6 ActiveX component with a class written in .NET. The new class is as follows:

<ComClass("cd74ab4a-76ca-4c84-9f49-147e6f6ac01f", "b3314f71-cb8d-48ea-bfe6-9d1995aa4f58", "40c30052-0cc8-4ef6-b1e8-92e4ddbcb515")> _
Public Class MyNetClass

    Public Sub New()
        MyBase.New()
    End Sub

    Public Function MyMethod(ByRef arr() As String, ByRef arr2() As String, ByRef result As String) As Short
        result = String.Join(", ", arr) + " from .NET"
        Return 0
    End Function

End Class

I am testing it by replacing Set obj = CreateObject("MyHelloWorld.MyClass")with Set obj = CreateObject("HelloWorldCOMNet.MyNetClass")in a test application.

.NET-, , .

MyMethod, FatalExecutionEngineException.

arr2. ReDim arr2(0), .NET. , , . , VB6.

OleWoo, ( id).

.NET ?

: OleWoo:

[id(0x00000001)]
short MyMethod(
   [in, out] SAFEARRAY(BSTR)* arr,
   [in, out] SAFEARRAY(BSTR)* arr2,
   [in, out] BSTR* result
);
+4
1

, .NET Framework.

VB6. : Stack Trace with DispatchInfo :: IsVariantByrefStaticArray at the top

: Disassembled code showing that the pointer is dereferenced with offset 2

DispatchInfo:: IsVariantByrefStaticArray. (*V_ARRAYREF(pOle)), , NULL. fFeatures 2, 0x00000002.

, , . , .

+1

Source: https://habr.com/ru/post/1686229/


All Articles