c# 调用C++ 结构体指针的问题: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
c++ 结构体:
typedef struct _vehicles_info
{
int idx;
/*
* 车牌号码
*/
char vehicle_name[32];
/*
* rtsp信息
*/
char vehicle_rtsp[256];
//0 offline, 1 online
Byte status;
}VehicleInfo;
方法:int GetVehiclesInfo(VehicleInfo *pVehicle, int size);
转为C#方法后的代码如下:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public unsafe struct VehicleInfo
{
public int idx;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string vehicle_name;//车牌号码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string vehicle_rtsp;//rtsp信息
public byte bstatus;//0 offline, 1 online
};
方法:
[DllImport("UserClient.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int GetVehiclesInfo(IntPtr[] p, int size);
调用代码如下:
IntPtr[] record = new IntPtr[isize]; //申请内存空间
record[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BwsPtzManager.VehicleInfo)) * isize);
int realcount = BwsPtzManager.GetVehiclesInfo(record, isize);
for (int i = 0; i < realcount; i++)
{
BwsPtzManager.VehicleInfo vehinfo = new BwsPtzManager.VehicleInfo();
vehinfo = (BwsPtzManager.VehicleInfo)Marshal.PtrToStructure((IntPtr)((UInt32)record[i]), typeof(BwsPtzManager.VehicleInfo));
Marshal.FreeHGlobal(record[i]);
}
问题来了:代码运行到 vehinfo = (BwsPtzManager.VehicleInfo)Marshal.PtrToStructure((IntPtr)((UInt32)record[i]), typeof(BwsPtzManager.VehicleInfo)); 这里的时候,总是报 “尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的错误,求大神指点。已经折腾的快挂了。总找不到原因。
另附c++代码如下:
VehicleInfo *pVehicle = (VehicleInfo*)malloc(sizeof(VehicleInfo)*count) ;
memset(pVehicle, 0x00, sizeof(VehicleInfo)*count);
int realcount = GetVehiclesInfo(pVehicle, count);
printf("%d\n", realcount);
for(int i = 0; i < realcount; i++)
{
printf("%s,%s,%d\n", pVehicle[i].vehicle_name,pVehicle[i].vehicle_rtsp,pVehicle[i].status );
}
运行一切正常,没任何问题。神救救我吧
追问:首先很感谢您的回答,很详细.我还有个疑问
int GetVehiclesInfo(VehicleInfo *pVehicle, int size);
这个接口既然有问题的话,C++代码调用为何没有问题呢?因为这个接口不是我写的,所以要让别人改接口,必须证明他这个写法确实是错的.