C#中的Marshall C ++结构

时间:2014-09-21 15:26:43

标签: c# c++ struct marshalling

我在这里提到了类似的问题,但没有解决我的问题。

编组问题。我尝试了C ++结构到C#,但不能这样做。我在pinvoke.net搜索了任何解决方案的提示,但找不到任何东西。请帮帮我!

错误消息

 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;

已转换的结构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;
}

我在编组指向结构的指针时遇到错误。

C#代码

    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#

    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);

       //...
    }

我认为,结构转换错误并可能导致错误。等待帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

结构声明似乎很好,但请确保您正确理解SDK。在SDK_ALARM_INFO参数中返回pBuf的可能性很高,而不是dwUser。通常当某些SDK允许您使用用户定义的指针注册回调时,该指针会传递给回调方法(在这种情况下为dwUser),所以我认为在您的情况下dwUser实际上等于{ {1}}。

尝试将回调和方法声明更改为

this.Handle

并致电

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

这可能会有所帮助。