How to get byte[] from IntPtr in C#

2020-04-18 06:58发布

问题:

I want to pass a IntPtr to a method takes a byte[] Parameter in c#. Is that possible and if it is possible how can I do that?

thx

回答1:

Check out the Marshal.Copy method.

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);