所以我试图在我的.net项目中使用最初用C ++编写的代码。我认为最简单的方法就是将其包装在DLL中并使用pinvoke使其正常工作。
这似乎很简单,但每次都会在C ++代码的同一行引发堆损坏异常。
public class NN_Wrapper
{
[DllImport("MyNative.dll", CharSet= CharSet.Ansi)]
private static extern unsafe int DoWork(DataPoint[] fixedVals, int fixedValsLength, [In, Out]DataPoint[] workVals, int workValsLength);
public void DoWork(DataPoint[] fixedVals, DataPoint[] workVals)
{
DoWork(fixedVals, fixedVals.Length, workVals, workVals.Length);
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DataPoint
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public double[] inputVals;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public double[] outputVals;
}
但是大约两次调用DLL,它会抛出一个除外,我也得到了DLL的源代码,这就是它抛出了这个激励。
vertex *initPoints(double *x, double *y, double *z,
double *u, double *v, double *w, int n)
{
vertex* ps = new vertex[n];
当然,当我在测试C ++项目中运行代码时,它会毫无问题地执行,这是我的错误吗?