Marshalling int[] to SafeArray seems to behave differently in Unity and in a standalone .NET application.
I have a COM dll that defines an interface
[ComImport]
public interface Calibration
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_INT)]
Array getInputKey();
}
In my C# script, either in Unity or in a standalone .NET application, I created my class that inherits from Calibration
public class MyCalibration : Calibration
{
private static readonly int[] _inputs = new[] { 0 };
public void doSomethingElse()
{
// Something
}
public Array getInputKey()
{
return _inputs;
}
}
Another COM object has a method
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void setCalibrationInterface([MarshalAs(UnmanagedType.Interface), In] Calibration ical);
I created an instance of MyCalibration and used it as the argument to the setCalibrationInterface method.
var myCal = new MyCalibration();
util.setCalibrationInterface(myCal);
The doSomethingElse() gets called without a problem. However in Unity I got the following exception when getInputKey() gets called in the Unity Console:
MarshalDirectiveException: The type 'System.Array' layout needs to be Sequential or Explicit
(wrapper native-to-managed) SREYELINKLib.ICalibration.getInputKey(intptr,System.Array&)
UnityEngine.<>c:<RegisterUECatcher>b__0_0(Object, UnhandledExceptionEventArgs)
Any ideas how to fix this?