C # Marshal.Copy Intptr for 16-bit managed unsigned integer array

Why is there no overload in the C # Marshal.Copy program for copying from an unmanaged memory pointer to a 16-bit unsigned unsigned array?

Example:

Copy(IntPtr, Byte[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array. Copy(IntPtr, Char[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed character array. Copy(IntPtr, Double[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array. Copy(IntPtr, Int16[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array. Copy(IntPtr, Int32[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array. Copy(IntPtr, Int64[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array. Copy(IntPtr, IntPtr[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed IntPtr array. Copy(IntPtr, Single[], Int32, Int32). Copies data from an unmanaged memory pointer to a managed single-precision floating-point number array. 

If there is no alternative to sorting, how do I copy an unmanaged ushort array to a ushort managed array?

+3
source share
3 answers

Using unsafe code:

 public static unsafe void Copy(IntPtr ptrSource, ushort[] dest, uint elements) { fixed(ushort* ptrDest = &dest[0]) { CopyMemory((IntPtr)ptrDest, ptrSource, elements * 2); // 2 bytes per element } } public static unsafe void Copy(ushort[] source, Intptr ptrDest, uint elements) { fixed(ushort* ptrSource = &source[0]) { CopyMemory(ptrDest, (Intptr)ptrSource, elements * 2); // 2 bytes per element } } [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)] static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length); 

It can be easily adapted for copying uint [] and ulong [] (adjust the number of bytes per element)

+1
source

I think Marshal does not have Copy(IntPtr, UInt16[], Int32, Int32) overload Copy(IntPtr, UInt16[], Int32, Int32) due to CLS compliance (the principle of observing CLS is that unsigned integer operations are not affected by consumers, I do not agree with this FWIW rule).

When sorting, all that matters is the size of the elements. Subscription is only a minor issue. I would either use Byte or Int16 and then do my own casting, or I would use unsafe pointers.

 IntPtr unmanagedArray = ... Int16[] destination0 = new Int16[ count ]; Marshal.Copy( unmanagedArray, destination0, 0, count ); UInt16[] destinion1 = destination0.OfType<UInt16>().ToArray(); // Linq extension method 
0
source

Probably because not all managed languages ​​have unsigned types. I do not think this is a good reason, but it is a reasonable explanation.

The cast should work before the unsigned parameter.

 Copy(addr, (Int16[])someUnsignedArray, 0, len); 
-1
source

Source: https://habr.com/ru/post/982717/


All Articles