CopyMemory

VB Signature:

Public Declare Auto Sub CopyMemory lib "kernel32.dll" Alias "CopyMemory"(destination As IntPtr, source As IntPtr, length As UInteger)

<DllImport("kernel32.dll", SetLastError:= True, EntryPoint:= "CopyMemory")>
Public Shared Sub CopyMemory(destination As IntPtr, source As IntPtr, length As UInteger)
End Sub

Sample Code:

// static void Main()
    // {
    //   const int size = 200;
    //   IntPtr memorySource = Marshal.AllocHGlobal(size);
    //   IntPtr memoryTarget = Marshal.AllocHGlobal(size);
    //   CopyMemory(memoryTarget,memorySource,size);
    // }

     [DllImport("kernel32.dll", SetLastError=true, EntryPoint="CopyMemory")
     public static void CopyMemory(void* destination, void* source, uint length);

     public static unsafe void Main()
      {
     int[] data = Enumerable.Range(1, 1000000);
     int size = data.Length * sizeof(int);
     byte[] dst = new byte[size]
     fixed (byte* pd = dst){ fixed(int* ps = data){ CopyMemory(pd, ps, size); } }
     Console.WriteLine("10 data result : {0}", BitConverter.ToString(dst, 0, 10));
     Console.WriteLine("Is assuming equals : {0}", BitConverter.ToInt32(dst, 0) == data[0] && 
                               BitConverter.ToInt32(dst, dst.GetUpperBound(0) - sizeof(int)) == data[data.GetUpperBound(0)]);
     Console.ReadKey();
      }

Sample Code VB:

Última actualización