Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可以从托管数组中获取指针
byte [] buffer = new byte[length + byteAlignment]; GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr ptr = bufferHandle.AddrOfPinnedObject();
有没有办法做相反的事情。从固定对象中获取字节数组而不复制?
当然,这就是Marshal.Copy目的 - 没有办法(嗯,没有复制一些品种就没有办法)以其他方式在托管和非托管状态之间获取内存......嗯,这不是 100% 正确,但我假设你不不想仅仅依靠 Win32/C 和 p/invoke 来复制内存。
Marshal.Copy
Marshal.Copy使用看起来像:
IntPtr addressOfThing = ....; byte[] buffer = new byte[...]; Marshal.Copy(addressOfThing, buffer, 0, bufferSize);