emxArray_real_T到C#struct加上初始化

时间:2013-02-22 11:14:42

标签: c# matlab pinvoke matlab-coder

我正在尝试为这个C#结构创建一个'构造函数'(包括初始尝试):

[StructLayout(LayoutKind.Sequential)]
public struct emxArray_real_T
{
public IntPtr data;
public IntPtr size;
public int allocatedSize;
public int numDimensions;
[MarshalAs(UnmanagedType.U1)]
public bool canFreeData;

public emxArray_real_T(double[] cSharpData)
{
    var arraySize = Marshal.SizeOf(cSharpData[0]) * cSharpData.Length;
    this.data = Marshal.AllocHGlobal(arraySize);
    // ????
    numDimensions = 1;
    canFreeData = false;
}
}

C对应的C结构如下所示:

typedef struct emxArray_real_T
{
   real_T *data;
   int32_T *size;
   int32_T allocated;
   int32_T numDimensions;
   boolean_T canFreeData;
} emxArray_real_T;

并解释为here

期待任何评论/答案。谢谢!

2 个答案:

答案 0 :(得分:1)

关于如何执行此操作,您有几个选择。您可以分配非托管内存。然后复制托管内存的内容。然后,当您调用本机代码返回时,可能会将其复制回来。

由于您的示例将canFreeData设置为false,我猜您想以其他方式执行此操作。那就是将托管内存传递给本机代码。为此,您需要将其固定以防止GC移动。

为了使这些方法中的任何一种起作用,我认为你需要一个包装类来管理本机内存或固定。以下是我如何处理固定方法:

[StructLayout(LayoutKind.Sequential)]
public struct emxArray_real_T
{
    public IntPtr data;
    public IntPtr size;
    public int allocatedSize;
    public int numDimensions;
    [MarshalAs(UnmanagedType.U1)]
    public bool canFreeData;
}

public class emxArray_real_T_Wrapper : IDisposable
{
    private emxArray_real_T value;
    private GCHandle dataHandle;
    private GCHandle sizeHandle;

    public emxArray_real_T Value {
        get { return value; } 
    }

    public emxArray_real_T_Wrapper(double[] data)
    {
        dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
        value.data = dataHandle.AddrOfPinnedObject();
        sizeHandle = GCHandle.Alloc(new int[] { data.Length }, GCHandleType.Pinned);
        value.size = sizeHandle.AddrOfPinnedObject();
        value.allocatedSize = data.Length;
        value.numDimensions = 1;
        value.canFreeData = false;
    }

    public void Dispose()
    {
        dataHandle.Free();
        sizeHandle.Free();
        GC.SuppressFinalize(this);
    }

    ~emxArray_real_T_Wrapper()
    {
        Dispose();
    }
}

答案 1 :(得分:0)

代码:

[StructLayout(LayoutKind.Sequential)]
public struct emxArray_real_T
{
    public IntPtr data;
    public IntPtr size;
    public int allocatedSize;
    public int numDimensions;
    [MarshalAs(UnmanagedType.U1)]
    public bool canFreeData;
}

public class emxArray_real_T_Wrapper : IDisposable
{
    private emxArray_real_T value;
    private GCHandle dataHandle;
    private GCHandle sizeHandle;

    public ref emxArray_real_T Value
    {
        get { return ref value; }
    }

    public double[] Data
    {
        get
        {
            double[] data = new double[value.allocatedSize];
            Marshal.Copy(value.data, data, 0, value.allocatedSize);
            return data;
        }
    }

    public emxArray_real_T_Wrapper(double[] data)
    {
        dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
        value.data = dataHandle.AddrOfPinnedObject();
        sizeHandle = GCHandle.Alloc(new int[] { 1, data.Length }, GCHandleType.Pinned);
        value.size = sizeHandle.AddrOfPinnedObject();
        value.allocatedSize = data.Length;
        value.numDimensions = 1;
        value.canFreeData = false;
    }

    public void Dispose()
    {
        dataHandle.Free();
        sizeHandle.Free();
        GC.SuppressFinalize(this);
    }

    ~emxArray_real_T_Wrapper()
    {
        Dispose();
    }

声明:

[DllImport("TestFunc.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void TestFunc(int n, double WnLow, double WnHigh, ref emxArray_real_T kernel);

使用它:

public double[] CalculateBandPassCoefficients(int order, double FreqCutoffNormLow, double FreqCutoffNormHigh)
{
    double[] kernel = new double[order];
    using (emxArray_real_T_Wrapper wb = new emxArray_real_T_Wrapper(kernel))
    {
        TestFunc(order, FreqCutoffNormLow, FreqCutoffNormHigh, ref kernelWrapper.Value);

        kernel = kernelWrapper.Data;
    }
    return kernel;
}