3

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

Tokk
  • 4,499
  • 2
  • 31
  • 47
user594810
  • 149
  • 4
  • 9
  • I have a job that copies around 50 TB of data to a SQL VDI. If I could wrap that unmanaged pointer with a temporary managed byte[] array, and save the extra copy, yeah, that'd be great. – Brain2000 Jan 05 '19 at 04:49

1 Answers1

0

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);
cordellcp3
  • 3,557
  • 1
  • 17
  • 14
  • Is `Dump` an extension method of yours? The line containing that method should probably be deleted. –  Mar 14 '11 at 15:37
  • @Rest Wing: Sorry thats from Linq-Pad :) – cordellcp3 Mar 15 '11 at 08:47
  • 5
    Don't you think he means the other way around? He has an IntPtr pointing to data and he wants to pass it into a function declared with a byte[] parameter? – Dan Byström Mar 15 '11 at 10:46