shangshang_A
2019-07-24 17:12
采纳率: 0%
浏览 963

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

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条回答 默认 最新

相关推荐 更多相似问题