Calling unmanaged C++ library (dll) from C# creates an access violation error (0xc0000005)

前端 未结 2 1427
心在旅途
心在旅途 2021-01-06 10:23

Sorry for the long question. I just wanted to include everything that I know about the problem at the moment.

I\'m using Visual Studio 2008 to create a Windows Form

2条回答
  •  北海茫月
    2021-01-06 10:49

    Your structs are marshalled incorrectly because you didn't declare the arrays quite right. You need to tell the marshaller that they are fixed length arrays.

    EDIT

    In my original answer I missed the addition error that the bool members were not marshalled correctly. The default marshalling is for the 4 byte Windows BOOL but you need 1 byte C++ bool. The code below now handles that correctly. Sorry for the confusion.

    public struct PDWaveSample
    {
        [MarshalAs(UnmanagedType.I1)]
        public bool bValid;                             
        public float fPressure;                         
        public float fDistance;                         
        [MarshalAs(UnmanagedType.LPArray, SizeConst=4)]
        public float[] fVel;
        [MarshalAs(UnmanagedType.LPArray, SizeConst=4)]
        public ushort[] nAmp
    }
    
    public struct PDWaveBurst {
        [MarshalAs(UnmanagedType.LPArray, SizeConst=4096)]
        public float[] fST;
        public float fWinFloor;
        public float fWinCeil;
        [MarshalAs(UnmanagedType.I1)]
        public bool bUseWindow;
        [MarshalAs(UnmanagedType.I1)]
        public bool bSTOk;
        [MarshalAs(UnmanagedType.I1)]
        public bool bGetRawAST;
        [MarshalAs(UnmanagedType.I1)]
        public bool bValidBurst;
    }
    

提交回复
热议问题