简体   繁体   English

C#编组结构数组-FatalExecutionEngineError

[英]C# marshalling struct array - FatalExecutionEngineError

This function in the Oculus SDK gives error code 0xc0000005: Oculus SDK中的此函数给出错误代码0xc0000005:

[DllImport(LibFile)]
private static extern void ovrHmd_GetRenderScaleAndOffset(ovrFovPort fov,
                                                          ovrSizei textureSize,
                                                          ovrRecti renderViewport,
                                                          [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)]
                                                          [Out] out ovrVector2f[] uvScaleOffsetOut);

No other PInvoke functions throw errors, but I think this one's different because the output is an array. 没有其他PInvoke函数会引发错误,但是我认为这有所不同,因为输出是一个数组。 Actually there's one other function that returns an array, and it gives the same error: 实际上,还有另一个函数返回一个数组,并且给出相同的错误:

[DllImport(LibFile)]
private static extern void ovrHmd_GetEyeTimewarpMatrices(IntPtr hmd, ovrEyeType eye, ovrPosef renderPose,
                                                         [MarshalAs(UnmanagedType.LPArray, SizeConst = 2)]
                                                         [Out] out ovrMatrix4f_Raw[] twnOut);

Here are the struct declarations: 这是结构声明:

[StructLayout(LayoutKind.Sequential)]
public struct ovrVector2f
{
    public float x, y;
}

[StructLayout(LayoutKind.Sequential)]
public struct ovrMatrix4f_Raw
{
    public float m00;
    public float m01;
    public float m02;
    public float m03;

    public float m10;
    public float m11;
    public float m12;
    public float m13;

    public float m20;
    public float m21;
    public float m22;
    public float m23;

    public float m30;
    public float m31;
    public float m32;
    public float m33;
}

Looking at the SDK source, I know at least ovrHmd_GetRenderScaleAndOffset looks pretty boring, I can't imagine the error would come from inside the SDK. 从SDK的源代码来看,我至少知道ovrHmd_GetRenderScaleAndOffset看起来很无聊,我无法想象该错误将来自SDK内部。

I feel like I need to specify sizes somewhere in the function signature? 我觉得我需要在函数签名中的某个地方指定大小吗? And I'm wondering if it's limited to output parameters, or any and all "array of struct" parameters. 我想知道它是否仅限于输出参数,或任何和所有“结构数组”参数。

The array parameters are declared incorrectly. 数组参数声明不正确。 The use of out introduces a spurious extra level of indirection. 使用out引入虚假的间接附加级别。 You need to pass an array, that you allocate, and let the unmanaged code populate it. 您需要传递一个分配的数组,然后让非托管代码填充它。 Declare the parameter like this 这样声明参数

private static extern void ovrHmd_GetRenderScaleAndOffset(
    ovrFovPort fov,                                       
    ovrSizei textureSize,
    ovrRecti renderViewport,
    [Out] ovrVector2f[] uvScaleOffsetOut
);

Allocate the array before calling the function: 在调用函数之前分配数组:

ovrVector2f[] uvScaleOffsetOut = new ovrVector2f[2];

FWIW it would have helped had you shown the unmanaged side of the interface. FWIW,如果您显示界面的不受管侧,那将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM