2
votes

I have a VB6 assembly which I need to use in my .NET application and generated the Interop DLL for usage with .NET via tlbimp.exe.

The VB6 assembly has a function that has a byref array parameter. I don't want to change anything in the VB6 assembly, so I hope there is a solution to get the following working.

It is filling the array and I want to use it in my .NET code (c# or vb.net).

Example of the VB6 function (file NativeClass.cls):

Public Function GetData(ByRef data() As String) As Integer
    Dim tResults() As String
    Dim sRecordCount As String
    Dim lCount As Long

    ' load data
    sRecordCount = dataDummyObject.RecordCount

    ReDim tResults(sRecordCount, 2)

    ' fill the array in a loop
    For lCount = 0 To sRecordCount - 1
        tResults(lCount, 0) = dataDummyObject.Fields("property1")
        tResults(lCount, 1) = dataDummyObject.Fields("property2")

        If (sRecordCount - 1 - lCount) > 0 Then
            Call dataDummyObject.MoveNext
        End If
    End For

    data = tResults
    GetData = sRecordCount
End Function

Now I want to use it from VB.NET:

Private _nativeAssembly As New NativeClass()

Public Function GetDataFromNativeAssembly() As String()
    Dim loadedData As String() = Nothing

    _nativeAssembly.GetData(loadedData)

    Return loadedData
End Function

C# version:

private NativeClass _nativeAssembly = null;

public string[] GetDataFromNativeAssembly()
{
    string[] loadedData = null;

    _nativeAssembly.GetData(loadedData);

    return loadedData;
}

But when executing the code I get following Exception:

System.Runtime.InteropServices.SafeArrayRankMismatchException: SafeArray of rank 2 has been passed to a method expecting an array of rank 1.

I really need help to solve this problem! Thanks for any piece of advice!

2

2 Answers

2
votes

I don't think you can solve this without modifying the VB6 code. Try declaring the function as

Public Function GetData(ByRef data As Variant) As Integer

or

Public Function GetData(ByRef data As Object) As Integer

The ReDim to string array should work fine from Variant. I remember doing it like this all the time because of VB6 not letting a 2D array as a parameter.

When inspecting it from .NET you should see the type. I don't have a VB6 IDE on my machine to verify this.

If one works you should be able to cast over to the String(,) you expect.

1
votes

This is air code, but you could try this in the VB.Net? Note the additional comma to indicate a 2-D array.

Dim loadedData As String(,) = Nothing