本文整理汇总了C#中System.Runtime.InteropServices.Marshal.Copy方法的典型用法代码示例。如果您正苦于以下问题:C# Marshal.Copy方法的具体用法?C# Marshal.Copy怎么用?C# Marshal.Copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.InteropServices.Marshal
的用法示例。
在下文中一共展示了Marshal.Copy方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Create a managed array.
Int64[] managedArray = { 1, 2, 3, 4 };
// Initialize unmanaged memory to hold the array.
int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(managedArray, 0, pnt, managedArray.Length);
// Copy the unmanaged array back to another managed array.
Int64[] managedArray2 = new Int64[managedArray.Length];
Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
Console.WriteLine("The array was copied to unmanaged memory and back.");
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
}
}
示例2: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Create a managed array.
int[] managedArray = { 1, 2, 3, 4 };
// Initialize unmanaged memory to hold the array.
int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(managedArray, 0, pnt, managedArray.Length);
// Copy the unmanaged array back to another managed array.
int[] managedArray2 = new int[managedArray.Length];
Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
Console.WriteLine("The array was copied to unmanaged memory and back.");
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
}
}
示例3: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Create a managed array.
short[] managedArray = { 1, 2, 3, 4 };
// Initialize unmanaged memory to hold the array.
int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(managedArray, 0, pnt, managedArray.Length);
// Copy the unmanaged array back to another managed array.
short[] managedArray2 = new short[managedArray.Length];
Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
Console.WriteLine("The array was copied to unmanaged memory and back.");
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
}
}
示例4: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Create a managed array.
double[] managedArray = { 0.1, 0.2, 0.3, 0.4 };
// Initialize unmanaged memory to hold the array.
int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(managedArray, 0, pnt, managedArray.Length);
// Copy the unmanaged array back to another managed array.
double[] managedArray2 = new double[managedArray.Length];
Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
Console.WriteLine("The array was copied to unmanaged memory and back.");
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
}
}
示例5: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Create a managed array.
byte[] managedArray = { 1, 2, 3, 4 };
// Initialize unmanaged memory to hold the array.
int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(managedArray, 0, pnt, managedArray.Length);
// Copy the unmanaged array back to another managed array.
byte[] managedArray2 = new byte[managedArray.Length];
Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
Console.WriteLine("The array was copied to unmanaged memory and back.");
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
}
}
示例6: Main
// Remember that the actual size of System.Char in unmanaged memory is 2.
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main()
{
// Create a managed array.
char[] managedArray = new char[1000];
managedArray[0] = 'a';
managedArray[1] = 'b';
managedArray[2] = 'c';
managedArray[3] = 'd';
managedArray[999] = 'Z';
// Initialize unmanaged memory to hold the array.
// int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length; // Incorrect
int size = Marshal.SystemDefaultCharSize * managedArray.Length; // Correct
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(managedArray, 0, pnt, managedArray.Length);
// Copy the unmanaged array back to another managed array.
char[] managedArray2 = new char[managedArray.Length];
Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
Console.WriteLine("Here is the roundtripped array: {0} {1} {2} {3} {4}",
managedArray2[0], managedArray2[1], managedArray2[2], managedArray2[3],
managedArray2[999]);
Console.WriteLine("The array was copied to unmanaged memory and back.");
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
}
}