我试图在C#中将double []转换为IntPtr。这是我要转换的数据:
double[] rotX = { 1.0, 0.0, 0.0 };
double[] rotY = { 0.0, 1.0, 0.0 };
double[] rotZ = { 0.0, 0.0, 1.0 };
这是我要提供的函数IntPtr,它是从上面的数组转换而来的:
SetRotationDirection(IntPtr rotX, IntPtr rotY, IntPtr rotZ);
我应该怎么做?
最佳答案
using System.Runtime.InteropServices;
/* ... */
double[] rotX = { 1.0, 0.0, 0.0 };
double[] rotY = { 0.0, 1.0, 0.0 };
double[] rotZ = { 0.0, 0.0, 1.0 };
var gchX = default(GCHandle);
var gchY = default(GCHandle);
var gchZ = default(GCHandle);
try
{
gchX = GCHandle.Alloc(rotX, GCHandleType.Pinned);
gchY = GCHandle.Alloc(rotY, GCHandleType.Pinned);
gchZ = GCHandle.Alloc(rotZ, GCHandleType.Pinned);
SetRotationDirection(
gchX.AddrOfPinnedObject(),
gchY.AddrOfPinnedObject(),
gchZ.AddrOfPinnedObject());
}
finally
{
if(gchX.IsAllocated) gchX.Free();
if(gchY.IsAllocated) gchY.Free();
if(gchZ.IsAllocated) gchZ.Free();
}
关于c# - 编码(marshal)double []到C#中的IntPtr吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18966902/