Marshal 类
来源:互联网 发布:windows优化大师 win8 编辑:程序博客网 时间:2024/05/16 12:09
功能及位置 :
将数据从托管对象封送到非托管内存块,属于.NET Framework 类库
命名空间:System.Runtime.InteropServices
程序集:mscorlib(在 mscorlib.dll 中)
提供了一个方法集,这些方法用于分配非托管内存、复制非托管内存块、将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法。
命名空间:System.Runtime.InteropServices
程序集:mscorlib(在 mscorlib.dll 中)
C#
C++
VB
public static class Marshal
J#
public final class Marshal
JScript
public final class Marshal
Marshal 类中定义的 static 方法对于处理非托管代码至关重要。此类中定义的大多数方法通常由需要在托管和非托管编程模型之间提供桥梁的开发人员使用。例如,StringToHGlobalAnsi 方法将 ANSI 字符从指定的字符串(在托管堆中)复制到非托管堆中的缓冲区。该方法还分配大小正确的目标堆。
公共语言运行库提供了特定的封送处理功能。有关封送处理行为的详细信息,请参见 互操作封送处理。
下面的代码示例演示如何使用 Marshal 类所定义的各个方法。
C#
C++
using System;using System.Text;using System.Runtime.InteropServices;public struct Point{ public Int32 x, y;}public sealed class App{ static void Main() { // Demonstrate the use of public static fields of the Marshal class. Console.WriteLine("SystemDefaultCharSize={0}, SystemMaxDBCSCharSize={1}", Marshal.SystemDefaultCharSize, Marshal.SystemMaxDBCSCharSize); // Demonstrate the use of the SizeOf method of the Marshal class. Console.WriteLine("Number of bytes needed by a Point object: {0}", Marshal.SizeOf(typeof(Point))); Point p = new Point(); Console.WriteLine("Number of bytes needed by a Point object: {0}", Marshal.SizeOf(p)); // Demonstrate how to call GlobalAlloc and // GlobalFree using the Marshal class. IntPtr hglobal = Marshal.AllocHGlobal(100); Marshal.FreeHGlobal(hglobal); // Demonstrate how to use the Marshal class to get the Win32 error // code when a Win32 method fails. Boolean f = CloseHandle(new IntPtr(-1)); if (!f) { Console.WriteLine("CloseHandle call failed with an error code of: {0}", Marshal.GetLastWin32Error()); } } // This is a platform invoke prototype. SetLastError is true, which allows // the GetLastWin32Error method of the Marshal class to work correctly. [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)] static extern Boolean CloseHandle(IntPtr h); }// This code produces the following output.// // SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1// Number of bytes needed by a Point object: 8// Number of bytes needed by a Point object: 8// CloseHandle call failed with an error code of: 6
System.Object
System.Runtime.InteropServices.Marshal
System.Runtime.InteropServices.Marshal
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0、1.1、1.0.NET Compact Framework
受以下版本支持:2.0、1.0参考
Marshal 成员System.Runtime.InteropServices 命名空间
0 0
- Marshal类
- Marshal 类
- Marshal 类
- Marshal 类
- Marshal 类
- Marshal类
- Marshal类的使用
- [.NET2.0]Marshal类的使用
- struct和byte[]相互转换(用Marshal类实现)
- struct和byte[]相互转换(用Marshal类实现)
- window应用程序靠消息驱动、Marshal类、StructureToByte、ByteToStructure
- Marshal Class
- Marshal.GetDelegateForFunctionPointer
- 什么是marshal
- Marshal C#
- Marshal.AllocHGlobal VS Marshal.AllocCoTaskMem, Marshal.SizeOf VS sizeof()
- Marshal.GetDelegateForFunctionPointer 方法和Marshal.GetFunctionPointerForDelegate 方法
- Marshal类 两个方法StructureToPtr和PtrToStructure实现序列化 字节 数组 转换
- 求无向无权图起点到终点的所有路径
- warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory
- Win7下安装was6.1报错解决办法
- 假如有了android电脑系统
- Posix标准中的timer和AIO
- Marshal 类
- Ext中的样式控制
- hive修改表模式<转>
- Makefile相关知识
- 如何开始一个项目
- LeetCode - Remove Duplicates from Sorted List
- sql ldf文件过大 解决方案
- 赋值函数
- 2014年TOP10的PHP框架