我需要从C非托管代码中获取值。实际上,我从非托管调用函数,函数的返回类型是点,其中点是结构。
下面提到的结构
typedef struct point
{
Poly* pol;
NL_DEGREE p;
VECTOR* vec;
} Point;其中Poly和VECTOR是结构。
实际上,当IntPtr在C#.After中得到IntPtr值时,我得到了返回值点,我尝试将这个Intptr转换为Array。以下面的方式转换Array。
point[] Q = new point[2];
int size= Marshal.SizeOf(new point());
for (int i = 0; i < 2; i++)
{
Q[i] = (point)Marshal.PtrToStructure(new IntPtr(Qptr.ToInt32() + (i * size)), typeof(point));
}但是在得到数组后,每个结构元素的值就变成了null.What --我做错了,请有人建议我.
我在下面提到了在c#中创建的结构。
public unsafe struct point
{
public Poly* pol;
public NL_DEGREE p;
public vECTOR* knt;
}其中聚
public unsafe struct Poly
{
public Int32 n;
public cpoint* Pw;
}coint也是一个结构
public struct cpoint
{
public double x;
public double y;
public double z;
public double w;
}其中向量
public unsafe struct VECTOR
{
public Int32 m;
public double *U;
}发布于 2016-02-24 06:22:22
从您的代码片段来看,尚不清楚在C中如何定义结构。假设您的C#定义应该是默认的对齐方式,则如下所示:
public class Point
{
IntPtr pol;
NL_DEGREE p;
IntPtr vec;
}您仍然需要考虑具有适当包大小的精确结构布局(请参阅1 )
因此,在您的代码中,您必须执行以下操作:
var point = (Point)Marshal.PtrToStructure(ptr, typeof(Point));
var poly = (Poly)Marshal.PtrToStructure(point.pol, typeof(Poly));
var vector = (Vector)Marshal.PtrToStructure(point.vec, typeof(Vector));由于保利和向量可能都是数组,所以您可能希望使用IntPtr.Add() 2方法来迭代这些数组。
https://stackoverflow.com/questions/35594401
复制相似问题