小智 198
其他方式,
GCHandle pinnedArray = GCHandle.Alloc(byteArray, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
// Do your stuff...
pinnedArray.Free();
Run Code Online (Sandbox Code Playgroud)
Mic*_*ier 122
这应该工作但必须在不安全的环境中使用:
byte[] buffer = new byte[255];
fixed (byte* p = buffer)
{
IntPtr ptr = (IntPtr)p;
// do you stuff here
}
Run Code Online (Sandbox Code Playgroud)
要注意,你必须使用固定块中的指针!一旦你不再在固定块中,gc就可以移动对象.
Ric*_*lay 88
不确定将IntPtr添加到数组,但是您可以使用Mashal.Copy复制数据以用于非托管代码:
IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);
// Call unmanaged code
Marshal.FreeHGlobal(unmanagedPointer);
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用一个属性声明一个结构,然后使用Marshal.PtrToStructure,但这仍然需要分配非托管内存.
编辑:另外,正如Tyalis指出的那样,如果不安全的代码是你的选择,你也可以使用固定的
小智 18
您可以使用Marshal.UnsafeAddrOfPinnedArrayElement(array, 0)获取指向数组的内存指针.
dlc*_*ers 13
这是@ user65157的回答(+1为此,BTW):
我为固定对象创建了一个IDisposable包装器:
class AutoPinner : IDisposable
{
GCHandle _pinnedArray;
public AutoPinner(Object obj)
{
_pinnedArray = GCHandle.Alloc(obj, GCHandleType.Pinned);
}
public static implicit operator IntPtr(AutoPinner ap)
{
return ap._pinnedArray.AddrOfPinnedObject();
}
public void Dispose()
{
_pinnedArray.Free();
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
using (AutoPinner ap = new AutoPinner(MyManagedObject))
{
UnmanagedIntPtr = ap; // Use the operator to retrieve the IntPtr
//do your stuff
}
Run Code Online (Sandbox Code Playgroud)
我发现这是一个很好的方式,不要忘记调用Free():)
| 归档时间: |
|
| 查看次数: |
146987 次 |
| 最近记录: |