System.AccessViolationException dll C#HANTEK SDK

时间:2013-12-01 03:10:53

标签: c# c++

来自Hantek sdk的C ++

HTMARCH_API short WINAPI dsoReadHardData(WORD DeviceIndex,
                                         short* pCH1Data, 
                                         short* pCH2Data, 
                                         ULONG nReadLen,
                                         short* pCalLevel,
                                         int nCH1VoltDIV,
                                         int nCH2VoltDIV,
                                         short nTrigSweep,
                                         short nTrigSrc,
                                         short nTrigLevel,
                                         short nSlope,
                                         int nTimeDIV,
                                         short nHTrigPos,
                                         ULONG nDisLen,
                                         ULONG* nTrigPoint, 
                                         short nInsertMode);

C#

 [DllImport("HTMarch.dll", CharSet = CharSet.Unicode,CallingConvention = CallingConvention.StdCall)] 
 public static extern  short  dsoReadHardData(short DeviceIndex,
                                              short[] pCH1Data,
                                              short[] pCH2Data, 
                                              uint nReadLen,
                                              short[] pCalLevel,
                                              int nCH1VoltDIV,
                                              int nCH2VoltDIV,
                                              short nTrigSweep,
                                              short nTrigSrc,
                                              short nTrigLevel,
                                              short nSlope,
                                              int nTimeDIV,
                                              short nHTrigPos,
                                              uint nDisLen,
                                              uint nTrigPoint,
                                              short nInsertMode);

使用

int nReadLen = 10240;//10k
            int nDrawLen = 10000;
            short nTrigLevel = 64;
            short nSlope = 0;// 0:Rise; 1: Fall
            short nHTrigPos = 50;// 0 ~ 100
            uint  nTrigPoint = 0;
            short[] pCH1Data = new short[nReadLen];
            short[] pCH2Data = new short[nReadLen];
            short nRe = Hantek.dsoReadHardData(m_nDevIndex,
                                        pCH1Data,
                                        pCH2Data,
                                        (uint)nReadLen,
                                        m_nCalData,
                                        m_nCH1VoltDIV,
                                        m_nCH2VoltDIV,
                                        0,//0:AUOT; 1:Normal; 2: Signal
                                        0,//CH1
                                         nTrigLevel,
                                        nSlope,
                                        m_nTimeDIV,
                                        nHTrigPos,
                                        (uint)nDrawLen,
                                        nTrigPoint,
                                        0);

怎么了? 当programm停止pCH1Data和pCH2Data中的所有数据时。 尝试写包装但有错误的零值。

2 个答案:

答案 0 :(得分:0)

C声明中的第五个参数和DLLImport语句被输入为指向short / short的指针数组,但是在调用函数时传递的第五个参数名为m_nCalData,这意味着单个整数值。确保将short-array作为第五个参数传递。

答案 1 :(得分:0)

参数nTrigPoint是一个指向unsigned long的指针,你已经重新声明它是一个无符号整数,它需要是一个指针,你需要提供一个unsigned long的地址,因为该DLL函数将设置任何nTrigPoint指向触发点索引(相对于原始数据数组)。

参数pCalLevel需要是一个大小为32的短数组的地址,只需将其设置为0,或先用dsoGetCalLevel()填充它。