【发布时间】:2014-07-23 17:31:04
【问题描述】:
我必须从 C# 调用一个非托管函数,并且必须为其提供一个坐标数组(双精度)。对于这种情况,编组如何正确工作?
在非托管方面:
typedef struct dPoint3dTag
{
double x, y, z;
} dPoint3d;
void UnmanagedModifyGeometry(char *strFeaId, dPoint3d *pnts, int iNumPnts);
我在托管端为 DPoint3d 定义了一个托管结构:
[StructLayout(LayoutKind.Sequential)]
public struct DPoint3d
{
// Constructor
public DPoint3d(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public double x, y, z;
}
我正在尝试以这种方式从 C# 调用非托管函数:
// Import of the unmanaged function
[DllImport("Unmanaged.dll")]
public static extern void UnmanagedModifyGeometry([MarshalAs(UnmanagedType.LPStr)] string strFeaId, DPoint3d[] pnts, int iNumPnts);
// Using the unmanaged function from C#
// Allocating points
DPoint3d[] pnts = new DPoint3d[iPntCnt];
String strFeaId = "4711";
// After filling in the points call the unmanaged function
UnmanagedModifyGeometry(strFeaId, pnts, iPntCnt);
这个工作流程正确吗?
问候 汤姆托雷尔
【问题讨论】:
标签: c# pinvoke marshalling