2

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.

4
  • It is not specified in your question, but I assume you have created an object in C# that you have exposed to COM and now want to consume from VB6. Did you do so correctly? What is the method signature as it appears in the VB6 object browser? You also have two pairs of erroneous parentheses on the obj.CreateMultipleNewEsnBelegung (qpenrPos()) line, remove them. Commented Feb 1, 2023 at 8:57
  • Yes it's an object created in C#. The object works in VB without any problems. I know this because I already use it for other methods, but in these cases I do not pass it as an array, but as a simple object. Commented Feb 1, 2023 at 9:01
  • @GSerg Thank you. I have improved that. However, I still get this error message: invalid procedure call or invalid argument Commented Feb 1, 2023 at 10:08
  • How does the improved code look now and what is the method signature as it appears in the VB6 object browser? Commented Feb 1, 2023 at 12:38

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.