C#对接C++,回调函数返回IntPtr事件信息转成结构体时出错 5C

C#对接C++,回调函数返回IntPtr事件信息转成结构体时出错,指定结构必须能直接复制到本机结构中,或是具有布局信息。
private int RealLoadPicCallback(IntPtr lAnalyzerHandle, uint dwEventType, IntPtr pEventInfo, IntPtr pBuffer, uint dwBufSize, IntPtr dwUser, int nSequence, IntPtr reserved)
{
ZN_ALARM_TYPE type = (ZN_ALARM_TYPE)dwEventType;
switch (type)
{
case ZN_ALARM_TYPE.EVENT_IVS_CROSSLINEDETECTION:

                DEV_EVENT_CROSSLINE_INFO eventInfo = new DEV_EVENT_CROSSLINE_INFO();

                Marshal.Copy(pBuffer, data, 0, (int)dwBufSize);

                try
                {
                    var obj = Marshal.PtrToStructure(pEventInfo, typeof(DEV_EVENT_CROSSLINE_INFO));
                    eventInfo = null;
                }
                catch (Exception ex)
                {

                    throw ex;
                }

                break;
            default:
                // Console.WriteLine(lCommand.ToString("X"));
                break;
        }


        return 0;
    }

            结构体DEV_EVENT_CROSSLINE_INFO是只需要第一个nChannelID,所以将其他的字段合在buffer大小是1956
             public class DEV_EVENT_CROSSLINE_INFO
{
    public int nChannelID;               // 通道号
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1956)]
    public byte[] buffer;

    //public char szName;          
    //public char bReserved1;             
    //public double PTS;                           
    //public NET_TIME_EX UTC;                          
    //public int nEventID;                         
    //public DH_MSG_OBJECT stuObject;                       
    //public DH_EVENT_FILE_INFO stuFileInfo;                  
    //public DH_POINT DetectLine;                      
    //public int nDetectLineNum;                    
    //public DH_POINT TrackLine;   
    //public int nTrackLineNum;                  
    //public byte bEventAction;                       
    //public byte bDirection;                     
    //public byte byReserved;
    //public byte byImageIndex;                   
    //public uint dwSnapFlagMask;                    
    //public int nSourceIndex;                   
    //public char szSourceDevice;          
    //public int nOccurrenceCoun;             
    //public EVENT_INTELLI_COMM_INFO stuIntelliCommInfo;     
    //public byte bReserved;              
}

1个回答

SizeConst = 1956
你确认长度对么

报什么错

shangshang_A
shangshang_A 我用sizeof看了他的长度,整个结构体的长度是1960,nChannelID是int类型,所以1956应该没有问题,报的错误是指定结构必须能直接复制到本机结构中,或是具有布局信息
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问