1

I have a custom struct defined in MIDL:

struct Foo
{
    [string] char* Bar;
    int Baz;
}

I'm trying to pass this from a C# application to a C++ application. The interface assembly was generated with tlbimp.exe.

I should mention that I have next to no knowledge of MIDL and COM interop, but we're stuck using this interop method for the time being, so there's no way around that for now.

I tried various ways of defining an array of such a type, none with success. Ideally I'd want to call it on the C# side just like this:

ComObject.SomeFunction(someArray);

However, I may not be able to do that without a size parameter, so this would also be fine:

ComObject.SomeFunction(someArray.Length, someArray);

I tried the following ways to define it, none of which were successful:

HRESULT SomeFunction(struct Foo array[]); // For some reason it interprets this as a
                                          // ref parameter... I assume because it's
                                          // equivalent to a pointer?
HRESULT SomeFunction(struct Foo** array); // Tried with a pointer as well,
                                          // but I think the memory layout messes it up
HRESULT SomeFunction(int length, [size_is(length)] struct Foo array[]);
HRESULT SomeFunction(int length, [size_is(length)] struct Foo** array); // I don't know how
                                                                        // to marshal that on
                                                                        // the C# side

What's the right way to define this in the MIDL and to invoke it on the C# side? Ugly temporary solutions welcome, since this entire thing will be refactored anyway in two months, but we need this before then. For example, currently I'm considering passing in two arrays of simple types (char* and int) and then combine them into the appropriate structure on the C++ side.

Stjepan Bakrac
  • 1,047
  • 11
  • 18

0 Answers0