我在MIDL中定义了一个自定义结构:
struct Foo
{
[string] char* Bar;
int Baz;
}
我试图将它从C#应用程序传递给C ++应用程序。使用tlbimp.exe生成接口程序集。
我应该提到我接下来不了解MIDL和COM互操作,但我们暂时停止使用这种互操作方法,所以目前还没有办法解决这个问题。
我尝试了各种方法来定义这种类型的数组,但没有成功。理想情况下,我想在C#端调用它,就像这样:
ComObject.SomeFunction(someArray);
但是,如果没有尺寸参数,我可能无法做到这一点,所以这也没关系:
ComObject.SomeFunction(someArray.Length, someArray);
我尝试了以下方法来定义它,但都没有成功:
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
在MIDL中定义它并在C#端调用它的正确方法是什么?丑陋的临时解决方案受到欢迎,因为整个事情将在两个月内重构,但我们需要在此之前完成。例如,目前我考虑传入两个简单类型数组(char *和int),然后将它们组合到C ++端的相应结构中。