Hasan Cemre Ok
Hasan Cemre Ok

Reputation: 63

Marshall C++ structure in C#

I referred the similar questions in here but didn't get the solution for my problem.

Problem with marshalling. I tried C++ structs to C# but can't do it. i was searched any tips for solution in pinvoke.net but can'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++ Structs

    typedef struct SDK_ALARM_INFO
    {
        int nChannel;
        int iEvent;
        int iStatus;
        SDK_SYSTEM_TIME SysTime;
    }SDK_AlarmInfo;

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

Converted structs C# :

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

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

    [MarshalAs(UnmanagedType.Struct)]
    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'm getting error when marshalling pointer to structure.

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 Function C#

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

       [DllImport("NetSdk.dll")]
       public static extern void H264_DVR_SetDVRMessCallBack(fMessCallBack cbAlarmcallback, IntPtr lUser);

       //...
    }

I think, structure conversions wrong and error that may be caused. Waiting for helps. Thanks !

Upvotes: 3

Views: 709

Answers (1)

max
max

Reputation: 34447

Structure declarations seem fine, but make sure you understand that SDK correctly. There is a high chance that SDK_ALARM_INFO is returned in pBuf parameter, not in dwUser. Usually when some SDK allows you to register callback with user-defined pointer, that pointer is passed to callback method (dwUser in this case), so I think that in your case dwUser is actually equal to this.Handle.

Try to change callback 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));

Probably this will help.

Upvotes: 2

Related Questions