Marshall C ++ structure in C #

I have mentioned similar questions here but haven't found a solution for my problem.

Sorting problem. I have tried C ++ Structures in C # but cannot seem to do it. I've searched for any advice for a solution at but couldn't find anything. Please help me!

Error messages

 An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.

 Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


C ++ Structures

    typedef struct SDK_ALARM_INFO
        int nChannel;
        int iEvent;
        int iStatus;
        SDK_SYSTEM_TIME SysTime;

     typedef struct SDK_SYSTEM_TIME{
         int  year;
         int  month;
         int  day;
         int  wday;
         int  hour;
         int  minute;
         int  second;
         int  isdst;


Converted C # Structures :

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SDK_ALARM_INFO

    public int nChannel;
    public int iEvent;
    public int iStatus;

    public SDK_SYSTEM_TIME SysTime;

public struct SDK_SYSTEM_TIME
    public int year;   
    public int month;
    public int day;
    public int wday;
    public int hour;
    public int minute;
    public int second;
    public int isdst;


I am getting an error when specifying a marshalling to a struct.

C # code

    private XMSDK.fMessCallBack msgcallback;

    bool MessCallBack(int lLoginID, string pBuf,uint dwBufLen, IntPtr dwUser)
        SDK_ALARM_INFO ai = new SDK_ALARM_INFO();

        //getting error bottom line
        ai = (SDK_ALARM_INFO) Marshal.PtrToStructure(dwUser, typeof(SDK_ALARM_INFO)); // getting error this line           

        MessageBox.Show("Event: " + ai.iEvent.ToString() + " - Channel: " + ai.nChannel + " - GTime : " + ai.SysTime);

        return form.DealwithAlarm(lLoginID,pBuf,dwBufLen);

    public int InitSDK()

        msgcallback  = new XMSDK.fMessCallBack(MessCallBack);
        XMSDK.H264_DVR_SetDVRMessCallBack(msgcallback, this.Handle);



MessCallBack C # function

    class XMSDK {
       // ...
       public delegate bool fMessCallBack(int lLoginID, string pBuf, uint dwBufLen, IntPtr dwUser);

       public static extern void H264_DVR_SetDVRMessCallBack(fMessCallBack cbAlarmcallback, IntPtr lUser);



I think there is a wrong struct conversion and an error that could be thrown. Waiting helps. Thank!


source to share

1 answer

Structure declarations seem fine, but make sure you understand this SDK correctly. There is a high likelihood that it is SDK_ALARM_INFO

returned in a parameter pBuf

and not in dwUser

. Usually when some SDK allows you to register a callback with a user-defined pointer, that pointer is passed to the callback method ( dwUser

in this case), so I believe it is dwUser

actually equal in your case this.Handle


Try changing the callbacks and method declarations to

delegate  bool fMessCallBack(int lLoginID, IntPtr pBuf, uint dwBufLen, IntPtr dwUser);
bool MessCallBack(int lLoginID, IntPtr pBuf, uint dwBufLen, IntPtr dwUser);


and call

ai = (SDK_ALARM_INFO) Marshal.PtrToStructure(pBuf, typeof(SDK_ALARM_INFO));


This will probably help.



All Articles