//2.定义CAN信息帧的数据类型。
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct VCI_CAN_OBJ
        {
            public uint ID;
            public uint TimeStamp;
            public byte TimeFlag;
            public byte SendType;
            public byte RemoteFlag;//是否是远程帧
            public byte ExternFlag;//是否是扩展帧
            public byte DataLen;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = UnmanagedType.I1)]
            public byte[] Data;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.I1)]
            public byte[] Reserved;
        }
 
[DllImport("ControlCAN.dll", EntryPoint = "VCI_Receive", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint VCI_Receive(uint DeviceType, uint DeviceInd, uint CANInd, ref VCI_CAN_OBJ pReceive, uint Len, int WaitTime);
CANApi.VCI_CAN_OBJ[] frameInfos = new CANApi.VCI_CAN_OBJ[50];
len = (int)CANApi.VCI_Receive(3, 0, 0, ref frameInfos[0], 50, 200);
                if (len <= 0)
                {                    CANApi.VCI_ReadErrInfo(3, 0, 0, ref errInfo);
                }运行程序可以读取并返回一条数据

解决方案 »

  1.   

    因为定义是ref VCI_CAN_OBJ pReceive,因此PInvoke就会认为只返回一条数据。如果要返回一个数组,请制定[Out]特性以及[MarshalAs(UnmanagedType.LPArray)]特性,并使用VCI_CAN_OBJ[] pReceive形式定义
      

  2.   

     ref VCI_CAN_OBJ pReceive,这不就是一个struct吗,不是数组
      

  3.   

    [DllImport("ControlCAN.dll", EntryPoint = "VCI_Receive", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern uint VCI_Receive(uint DeviceType, uint DeviceInd, uint CANInd, ref VCI_CAN_OBJ pReceive, uint Len, int WaitTime);是将这部分重新定义吗?[DllImport("ControlCAN.dll", EntryPoint = "VCI_Receive", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern uint VCI_Receive(uint DeviceType, uint DeviceInd, uint CANInd, out VCI_CAN_OBJ[] pReceive, uint Len, int WaitTime);
      

  4.   

    [Out][MarshalAs(UnmanagedType.LPArray)]VCI_CAN_OBJ[] pReceive
    这样