Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.
For a list of all members of this type, see Marshal Members.
NotInheritable Public Class Marshal
public sealed class Marshal
public __gc __sealed class Marshal
public class Marshal
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
The static (Shared in Visual Basic) methods defined on the Marshal class are essential to working with unmanaged code. Most methods defined here are typically used by advanced developers building custom marshalers who need to provide a bridge between the managed and unmanaged programming models. For example, the StringToHGlobalAnsi method copies the ANSI characters from a specified string (in the managed heap) to a buffer in the unmanaged heap. It also allocates the target heap of the right size, as the following C# code shows:
String s = "Hello";
IntPtr p = Marshal.StringToHGlobalAnsi(s);
The common language runtime provides specific marshaling capabilities. For details on marshaling behavior, see Interop Marshaling.
The Marshal class comprises many diverse members. The following table assigns each member to the category that best describes its usage.
|Category ||Members |
|Advanced marshaling ||GetManagedThunkForUnmanagedMethodPtr, GetUnmanagedThunkForManagedMethodPtr, NumParamBytes |
|COM library function ||BindToMoniker, GetActiveObject |
|COM utilities ||ChangeWrapperHandleStrength, CreateWrapperOfType, GetComObjectData, GetComSlotForMethodInfo, GetEndComSlot, GetMethodInfoForComSlot, GetStartComSlot, ReleaseComObject, SetComObjectData |
|Data transformation ||Managed to unmanaged: Copy, GetComInterfaceForObject, GetIDispatchForObject, GetIUnknownForObject, StringToBSTR, StringToCoTaskMemAnsi, StringToCoTaskMemAuto, StringToCoTaskMemUni, StringToHGlobalAnsi, StringToHGlobalAuto, StringToHGlobalUni, StructureToPtr, UnsafeAddrOfPinnedArrayElement |
Unmanaged to managed: Copy, GetObjectForIUnknown, GetObjectForNativeVariant, GetObjectsForNativeVariants, GetTypedObjectForIUnknown, GetTypeForITypeInfo, PtrToStringAnsi, PtrToStringAuto, PtrToStringBSTR, PtrToStringUni
Properties: SystemDefaultCharSize, SystemMaxDBCSCharSize
|Direct reading and writing ||ReadByte, ReadInt16, ReadInt32, ReadInt64, ReadIntPtr, WriteByte, WriteInt16, WriteInt32, WriteInt64, WriteIntPtr |
|Error handling ||COM: GetHRForException, ThrowExceptionForHR |
Win32: GetLastWin32Error, GetExceptionCode, GetExceptionPointers
|Hosting utilities ||GetThreadFromFiberCookie |
|IUnknown ||AddRef, QueryInterface, Release |
|Memory management ||COM: AllocCoTaskMem, ReAllocCoTaskMem, FreeCoTaskMem, FreeBSTR |
Win32: AllocHGlobal, ReAllocHGlobal, FreeHGlobal
|Platform invoke utilities ||Prelink, PrelinkAll, GetHINSTANCE |
|Structure inspection ||OffsetOf, SizeOf |
|Type information ||GenerateGuidForType, GenerateProgIdForType, GetTypeInfoName, GetTypeLibGuid, GetTypeLibGuidForAssembly, GetTypeLibLcid, GetTypeLibName, IsComObject, IsTypeVisibleFromCom |
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)
Marshal Members | System.Runtime.InteropServices Namespace