我有一个结构:
public struct ResultOfStrategy
{
......
public double[] param;
public IntPtr ptr;
}我将把这个结构放在DLL中,它需要在结构中返回数组的值
result = new ResultOfStrategy[(int)length];
for(int i = 0; i < (int)length; i++)
{
result[i].param = new double[10];
}
// try... finally trick to be sure that the code isn't interrupted by asynchronous exceptions
try
{
}
finally
{
handle = GCHandle.Alloc(result, GCHandleType.Pinned);
}
return handle.AddrOfPinnedObject();我得到一个错误-对象包含非原语或需要转换的数据。如何解决这个问题?
附注:我想从C#传入一个结构数组,其中有一个普通变量和一个双精度类型的数组。
发布于 2018-06-22 17:30:25
我不知道您到底想做什么,但有一件事是肯定的:您不能轻易地固定ResultOfStrategy结构。所以你必须创建一个struct的副本...
public struct ResultOfStrategy
{
public IntPtr ptr;
public double[] param;
}
public struct ResultOfStrategyImpl
{
public IntPtr ptr;
public IntPtr param;
}
public static IntPtr Test(int length, out GCHandle[] handlesToBeFreed)
{
var result = new ResultOfStrategy[(int)length];
for (int i = 0; i < (int)length; i++)
{
result[i].param = new double[10];
}
// Copy to new blittable object
var result2 = new ResultOfStrategyImpl[result.Length];
for (int i = 0; i < result.Length; i++)
{
result2[i].ptr = result[i].ptr;
}
handlesToBeFreed = new GCHandle[length + 1];
GCHandle handle;
// try... finally trick to be sure that the code isn't interrupted by asynchronous exceptions
try
{
}
finally
{
for (int i = 0; i < result.Length; i++)
{
handle = GCHandle.Alloc(result2[i], GCHandleType.Pinned);
handlesToBeFreed[i] = handle;
result2[i].param = handle.AddrOfPinnedObject();
}
handle = GCHandle.Alloc(result2, GCHandleType.Pinned);
handlesToBeFreed[result.Length] = handle;
}
return handle.AddrOfPinnedObject();
}
public static void FreeHandles(GCHandle[] handles)
{
if (handles != null)
{
for (int i = 0; i < handles.Length; i++)
{
handles[i].Free();
}
}
}像这样使用它:
GCHandle[] handlesToBeFreed = null;
try
{
IntPtr ptr = Test(10, out handlesToBeFreed);
// Work with ptr
}
finally
{
FreeHandles(handlesToBeFreed);
}https://stackoverflow.com/questions/50984351
复制相似问题