Search code examples
c#arrayscomvb6com-interop

Passing array from VB6 to C#.net


How to pass a VB6 array to C# through COM Interop?

I would like to call a method in VB6 that has an array as a parameter. Unfortunately VB complains about the inappropriate type. My C# code:

public void CreateMultipleNewEsnBelegung(ref QpEnrPos_COM[] Input);

public void CreateMultipleNewEsnBelegung(ref QpEnrPos_COM[] Input)
{
    List<Domain.Entities.QpEnrPos> qpEnrPos = new List<Domain.Entities.QpEnrPos>();
    foreach (var item in Input)
    {
        qpEnrPos.Add(ComConverter.ConvertQpEnrPosComToQpEnrPos(item));
    }
   Methods.CreateMultipleNewESNPos(qpEnrPos);
}

My VB code:

Dim qpenrPos(1) As CrossCutting_Application_ESN.QpEnrPos_COM

Set qpenrPos(0) = secondimportModel
Set qpenrPos(1) = firstimportModel

obj.CreateMultipleNewEsnBelegung (qpenrPos())

I know I need to do something with MarshalAs. However, I can't find the right way to do it.


Solution

  • I have managed to get it to work. The trick was to specify the array as an object array. But the content of the array can be filled with any data type.

    public void CreateMultipleNewEsnBelegung(ref object[] Input);
    
    public void CreateMultipleNewEsnBelegung(ref object[] Input)
    {
        List<Domain.Entities.QpEnrPos> qpEnrPos = new List<Domain.Entities.QpEnrPos>();
        foreach (var item in Input)
        {
            qpEnrPos.Add(ComConverter.ConvertQpEnrPosComToQpEnrPos(item));
        }
       Methods.CreateMultipleNewESNPos(qpEnrPos);
    }
    

    Vb Code:

    Dim qpenrPos(1) As Variant
    
    Set qpenrPos(0) = secondimportModel
    Set qpenrPos(1) = firstimportModel
    
    obj.CreateMultipleNewEsnBelegung (qpenrPos)