I'm trying to call a function in .NET from a native library, like this:
typedef int (WINAPI* TGetAllObjects)(int hModel, VARIANT& objects);
If you call it in the C++ client, the debugger shows that objects are a safearray of VARIANT, and each VARIANT is a safearray of BSTR.
I tried to implement as follows in C#:
public delegate TRetCode TGetAllObjects(int hModel,
[Out,In,MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT )] ref MyStruct[] objects);
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
public string[] Objects;
}
When calling the method, it gives an error:
System.Runtime.InteropServices.SafeArrayTypeMismatchException: "Specified array was not of the expected type."
Please tell me how to properly organize data marshalling in such a situation.