急!C#调用DLL传递结构体嵌套问题

.Net技术 码拜 7年前 (2017-04-14) 1697次浏览
C++代码如下:

struct DLL_DepthPoint
{
float x;              
float y;                
float z;               
float noise;           
uint16_t grayValue;      
uint8_t depthConfidence; //!< value 0 = bad, 255 = good
};
typedef struct DLL_DEPTHDATA
{
int                       version;         
long long  timeStamp;      
uint16_t                  width;         
uint16_t                  height;        
uint32_t*          exposureTimes;  
DLL_DepthPoint        points[65535];      
}DLL_DepthData;
DLL_GetRGBData(bool is_showGrey, DLL_DepthData *depth_data, unsigned char* rgb_buffer, int width, int height);

本人写的c#对应代码如下:

[StructLayout(LayoutKind.Sequential)]
 public  struct DLL_DepthPoint
 {
public float x;       
public float y;             
public float z;                 
public float noise;        
public UInt16 grayValue;  
public byte depthConfidence; 
   };
[StructLayout(LayoutKind.Sequential)]
 public  struct DLL_DEPTHDATA
{
public int version;       
public UInt32 timeStamp;  
public UInt16 width;         
public UInt16 height;       
public IntPtr  exposureTimes;  
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 38304)]
public DLL_DepthPoint[] points;    
  };
DLL_DEPTHDATA   depthData = new SafeNativeMethods.DLL_DEPTHDATA();
DLL_DepthPoint[] point;point = new SafeNativeMethods.DLL_DepthPoint[38304];
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(64));
Marshal.StructureToPtr(depthData.exposureTimes, ptr, false);
depthData.points = point;
IntPtr ptrRsult = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SafeNativeMethods.DLL_DEPTHDATA)));
Marshal.StructureToPtr(depthData, ptrRsult, false);
DLL_GetRGBData(false, ref ptrRsult, ref rgb_buffer[0], 224, 171);

现在调用起来程序直接退出,AccessViolation, 不清楚哪里出了问题,试了好多种方式。

解决方案

100

1、long long对应c#的long而不是int
2、数组长度明明是65535,真不知道是不是你本人写的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明急!C#调用DLL传递结构体嵌套问题
喜欢 (1)
[1034331897@qq.com]
分享 (0)