C++: 
typedef struct struct_nvr_devices_type
{
    char  type[20];    /* 设备类型字符串 支持的编码器类型/厂商标识 */
    char  fileName[100];  /* 仅供NVR内部使用的字段,表示该类型设备的接入库文件 */
    struct struct_nvr_devices_type *next;
}
bnclient_nvr_devices_type;_declspec(dllimport) int bnclient_getsupporteddevicestype(unsigned int u_sessionID, bnclient_nvr_devices_type *devType, int* p_size);

调用:
bnclient_nvr_devices_type s_devicetype, *devicetype=&s_devicetype;
int msize = 0;
int k=bnclient_getsupporteddevicestype(u_sessionID, devicetype,&msize);
while(k-- > 0){
                  this->m_devicetype.AddString(devicetype->type);
devicetype = devicetype->next;
     }
能够成功获取里面的信息C#: 
[StructLayout(LayoutKind.Sequential)]   
        public struct struct_nvr_devices_type
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string type;    /* 设备类型字符串 支持的编码器类型/厂商标识 */
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
            public string fileName;  /* 仅供NVR内部使用的字段,表示该类型设备的接入库文件 */
            public IntPtr next;
        }public static extern int bnclient_getsupporteddevicestype(int sessionID, IntPtr[] devtype, ref int p_size);
调用:IntPtr devicetype = new IntPtr[50]; //申请内存空间
devicetype[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BaoK_NetSDK.struct_nvr_devices_type)) * 50); 
k = BaoK_NetSDK.bnclient_getsupporteddevicestype(u_sessionID, devicetype, ref msize);
struct_nvr_devices_type[] NvrType = new struct_nvr_devices_type[k];
for (int i = 0; i <k; i++)
{
    NvrType[i] = new struct_nvr_devices_type();
    NvrType[i] = (struct_nvr_devices_type)Marshal.PtrToStructure(devicetype[i], typeof(struct_nvr_devices_type));
  }
其中k为获取信息的个数最后出现异常:尝试读取或写入受保护的内存。这通常指示其他内存已损坏

解决方案 »

  1.   

    P/Invoke……看到就头疼,哪句出错啊?
      

  2.   


    public static extern int bnclient_getsupporteddevicestype(int sessionID, IntPtr[] devtype, ref int p_size);
    我总觉得IntPtr[] 对应bnclient_nvr_devices_type结构体指针哪里不太对劲……
      

  3.   

    http://www.cnblogs.com/namek/archive/2010/08/26/1809247.html看这个
      

  4.   



    NvrType[i] = (struct_nvr_devices_type)Marshal.PtrToStructure(devicetype[i], typeof(struct_nvr_devices_type));
    这句话抛出的异常
      

  5.   


    public static extern int bnclient_getsupporteddevicestype(int sessionID, IntPtr devtype, ref int p_size);//这里我觉得应该这样而不是IntPtr[]
    调用:IntPtr devicetype =  Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BaoK_NetSDK.struct_nvr_devices_type))); //分配结构体大小的内存就可以了,*50干嘛不明白 k = BaoK_NetSDK.bnclient_getsupporteddevicestype(u_sessionID, devicetype, ref msize);
    struct_nvr_devices_type[] NvrType = new struct_nvr_devices_type[k];//这里你的原意是用数组来保存函数调用返回的结构体是吧?
    NvrType[0] = (struct_nvr_devices_type)Marshal.PtrToStructure(devicetype, typeof(struct_nvr_devices_type));
    int i = 1;
    struct_nvr_devices_type tmp = new struct_nvr_devices_type();
    tmp = NvrType[0];//用临时变量保存第一个结构体
    while(tmp.next != IntPtr.Zero)//实际上它是以链表的形式保存的
    {
    NvrType[i] = (struct_nvr_devices_type)Marshal.PtrToStructure(tmp.next, typeof(struct_nvr_devices_type));
    tmp = tmp.next;
    i++;
    }
    p/invoke不熟悉,不过我觉得这么写好像靠谱点,另外结构体的c#定义你确定没问题?仅供参考
      

  6.   

    我获取到数据了。就是把public static extern int bnclient_getsupporteddevicestype(int sessionID, IntPtr[] devtype, ref int p_size);
    中第二个参数改为Intptr了。谢谢大家帮忙了。
      

  7.   

    已经解决了。public static extern int bnclient_getsupporteddevicestype(int sessionID, IntPtr[] devtype, ref int p_size);
    我是把里面的第二参数改为IntPtr类型。然后获取到的。
    谢谢大家的帮忙了。