c# - 如何在 C# 中从 IntPtr 获取 byte[]

标签 c# bytearray intptr

我想将一个 IntPtr 传递给一个在 c# 中采用 byte[] 参数的方法。这可能吗?如果可能的话,我该怎么做?

谢谢

最佳答案

查看 Marshal.Copy方法。

byte[] managedArray = {1,2,3,4,5};
int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
Marshal.Copy(pnt, managedArray, 0 , managedArray.Length);

关于c# - 如何在 C# 中从 IntPtr 获取 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298930/

相关文章:

c# - 泛型接口(interface)的非泛型类实现不被继承

java - 获取 int 的字节表示,仅使用 3 个字节

c# - 调用第三方DLL中的方法

python - 如何在 Python 中创建 IntPtr?

c# - 获取IntPtr指向的数组的大小

c# - 我可以在 UWP 上运行一些 http 服务器吗?

c# - ResourceDictionary : {"Key cannot be null.\r\nParameter name: key"} 中的运行时错误

c# - 使用 Entity Framework 向模型中注入(inject)一些东西

python - 在 Python 中切片文件

java - 从套接字的 byte[] 开头解析 int