2014-09-05 43 views
0

此功能在魔環SDK提供了錯誤代碼0000005:C#編組結構陣列 - FatalExecutionEngineError

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

沒有其他的PInvoke函數拋出錯誤,但是我覺得這個人的不同,因爲輸出是一個數組。實際上,有一個返回數組另外一個功能,它給了同樣的錯誤:

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

這裏是結構聲明:

[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; 
} 

望着SDK源,我知道至少ovrHmd_GetRenderScaleAndOffset小艾無聊,我無法想象這個錯誤會來自SDK內部。

我覺得我需要在函數簽名的某處指定大小?我想知道它是否限於輸出參數,或任何和所有「結構數組」參數。

+0

4.5.2已經有Simd支持和向量類支持。 – Jay 2014-09-05 00:07:24

+0

是的,這很少會達到一個好的目的。您正在告訴pinvoke編組人員複製本地數組並將其銷燬。後者總是炸彈。只希望你的聲明錯誤,使用普通的'ovrVector2f [] uvScaleOffsetOut',沒有out屬性。並傳遞一個足夠大的數組。 – 2014-09-05 00:29:46

回答

2

數組參數聲明不正確。 out的使用引入了虛假的額外間接級別。您需要傳遞一個數組,分配給它,並讓非託管代碼填充它。聲明這樣

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

參數調用函數之前分配數組:

ovrVector2f[] uvScaleOffsetOut = new ovrVector2f[2]; 

FWIW它會幫助你了所示的界面的非託管方。