日期:2014-05-18  浏览次数:20756 次

C回调函数转换成C#
C/C++ code
typedef bool(__stdcall * TDevicelist_Callback)(Device_set * aset) ;
typedef  struct tagDevice_set
{
  Int  id;
  Char  _name[32];
  Char   username[32];
  Char   userpass[16];
  Char   ip [16];
  Uint   port;
  Byte   channel_count;
  Byte   start_channel;
  Byte   device_type;
}Device_set;



上述的回调函数如何转换成C#语言的

------解决方案--------------------
把指针用C#中的IntPtr代替,重写
------解决方案--------------------
public static extern int PXNVR_GetDeviceList(uint userid,IntPtr callback);
------解决方案--------------------
应该和C++差不多,参考下这个链接。
http://wenku.baidu.com/view/9f2ef117a216147917112813.html?st=1
------解决方案--------------------
C# code

        //类型定义
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        internal struct Device_set
        {
            public int id;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string _name;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string username;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string userpass;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string ip;
            public uint port;
            public byte channel_count;
            public byte start_channel;
            public byte device_type;
        }

        internal delegate bool TDevicelist_Callback(ref Device_set ds);
        //DLL导入
        [DllImport("DLL名字", CallingConvention = CallingConvention.StdCall)]
        internal static extern int PQNVR_GetDeviceList(uint userid, TDevicelist_Callback callback);