Marshal

MarshalCF 1.0

System.Runtime.InteropServices (mscorlib.dll)sealed class

This class offers a collection of static methods for working with unmanaged memory and converting managed types to unmanaged types. Unless you are developing specialized code for marshaling types between managed and unmanaged code, you probably do not need to use any of these methods.

GetHRForException( ) converts a .NET exception to a COM HResult. If you are curious about the platform you are running on, you can find out the size of a character with the SystemDefaultCharSize field, which is 1 on an ANSI platform (Windows 9x/Me) and 2 on a Unicode platform (Windows NT, 2000, and XP).

Use the IsComObject( ) method to determine whether an object is actually an unmanaged COM object. The AddRef( ) method increments a COM object's reference count.

public sealed class Marshal {
// Public Static Fields
   public static readonly int SystemDefaultCharSize;  
// =2
   public static readonly int SystemMaxDBCSCharSize;  
// =1
// Public Static Methods
   public static int AddRef(IntPtr pUnk);
   public static IntPtr AllocCoTaskMem(int cb);
   public static IntPtr AllocHGlobal(int cb);
   public static IntPtr AllocHGlobal(IntPtr cb);
   public static object BindToMoniker(string monikerName);
   public static void ChangeWrapperHandleStrength(object otp, bool fIsWeak);
   public static void Copy(byte[ ] source, int startIndex, IntPtr destination, int length);
   public static void Copy(char[ ] source, int startIndex, IntPtr destination, int length);
   public static void Copy(double[ ] source, int startIndex, IntPtr destination, int length);
   public static void Copy(short[ ] source, int startIndex, IntPtr destination, int length);
   public static void Copy(int[ ] source, int startIndex, IntPtr destination, int length);
   public static void Copy(long[ ] source, int startIndex, IntPtr destination, int length);
   public static void Copy(IntPtr source, byte[ ] destination, int startIndex, int length);
   public static void Copy(IntPtr source, char[ ] destination, int startIndex, int length);
   public static void Copy(IntPtr source, double[ ] destination, int startIndex, int length);
   public static void Copy(IntPtr source, short[ ] destination, int startIndex, int length);
   public static void Copy(IntPtr source, int[ ] destination, int startIndex, int length);
   public static void Copy(IntPtr source, long[ ] destination, int startIndex, int length);
   public static void Copy(IntPtr source, float[ ] destination, int startIndex, int length);
   public static void Copy(float[ ] source, int startIndex, IntPtr destination, int length);
   public static object CreateWrapperOfType(object o, Type t);
   public static void DestroyStructure(IntPtr ptr, Type structuretype);
   public static void FreeBSTR(IntPtr ptr);
   public static void FreeCoTaskMem(IntPtr ptr);
   public static void FreeHGlobal(IntPtr hglobal);
   public static Guid GenerateGuidForType(Type type);
   public static string GenerateProgIdForType(Type type);
   public static object GetActiveObject(string progID);
   public static IntPtr GetComInterfaceForObject(object o, Type T);
   public static object GetComObjectData(object obj, object key);
   public static int GetComSlotForMethodInfo(System.Reflection.MemberInfo m);
   public static int GetEndComSlot(Type t);
   public static int GetExceptionCode( );
   public static IntPtr GetExceptionPointers( );
   public static IntPtr GetHINSTANCE(System.Reflection.Module m);
   public static int GetHRForException(Exception e);
   public static int GetHRForLastWin32Error( );
   public static IntPtr GetIDispatchForObject(object o);
   public static IntPtr GetITypeInfoForType(Type t);
   public static IntPtr GetIUnknownForObject(object o);
   public static int GetLastWin32Error( );
   public static IntPtr GetManagedThunkForUnmanagedMethodPtr(IntPtr pfnMethodToWrap, 
         IntPtr pbSignature, int cbSignature);
   public static MemberInfo GetMethodInfoForComSlot(Type t, int slot, 
        ref ComMemberType memberType);
   public static void GetNativeVariantForObject(object obj, IntPtr pDstNativeVariant);
   public static object GetObjectForIUnknown(IntPtr pUnk);
   public static object GetObjectForNativeVariant(IntPtr pSrcNativeVariant);
   public static object[ ] GetObjectsForNativeVariants(IntPtr aSrcNativeVariant, int cVars);
   public static int GetStartComSlot(Type t);
   public static Thread GetThreadFromFiberCookie(int cookie);
   public static object GetTypedObjectForIUnknown(IntPtr pUnk, Type t);
   public static Type GetTypeForITypeInfo(IntPtr piTypeInfo);
   public static string GetTypeInfoName(UCOMITypeInfo pTI);
   public static Guid GetTypeLibGuid(UCOMITypeLib pTLB);
   public static Guid GetTypeLibGuidForAssembly(System.Reflection.Assembly asm);
   public static int GetTypeLibLcid(UCOMITypeLib pTLB);
   public static string GetTypeLibName(UCOMITypeLib pTLB);
   public static IntPtr GetUnmanagedThunkForManagedMethodPtr(IntPtr pfnMethodToWrap, 
        IntPtr pbSignature, int cbSignature);
   public static bool IsComObject(object o);
   public static bool IsTypeVisibleFromCom(Type t);
   public static int NumParamBytes(System.Reflection.MethodInfo m);
   public static IntPtr OffsetOf(Type t, string fieldName);
   public static void Prelink(System.Reflection.MethodInfo m);
   public static void PrelinkAll(Type c);
   public static string PtrToStringAnsi(IntPtr ptr);
   public static string PtrToStringAnsi(IntPtr ptr, int len);
   public static string PtrToStringAuto(IntPtr ptr);
   public static string PtrToStringAuto(IntPtr ptr, int len);
   public static string PtrToStringBSTR(IntPtr ptr);
   public static string PtrToStringUni(IntPtr ptr);
   public static string PtrToStringUni(IntPtr ptr, int len);
   public static object PtrToStructure(IntPtr ptr, Type structureType);
   public static void PtrToStructure(IntPtr ptr, object structure);
   public static int QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv);
   public static byte ReadByte(IntPtr ptr);
   public static byte ReadByte(IntPtr ptr, int ofs);
   public static byte ReadByte(in object ptr, int ofs);
   public static short ReadInt16(IntPtr ptr);
   public static short ReadInt16(IntPtr ptr, int ofs);
   public static short ReadInt16(in object ptr, int ofs);
   public static int ReadInt32(IntPtr ptr);
   public static int ReadInt32(IntPtr ptr, int ofs);
   public static int ReadInt32(in object ptr, int ofs);
   public static long ReadInt64(IntPtr ptr);
   public static long ReadInt64(IntPtr ptr, int ofs);
   public static long ReadInt64(in object ptr, int ofs);
   public static IntPtr ReadIntPtr(IntPtr ptr);
   public static IntPtr ReadIntPtr(IntPtr ptr, int ofs);
   public static IntPtr ReadIntPtr(in object ptr, int ofs);
   public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb);
   public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb);
   public static int Release(IntPtr pUnk);
   public static int ReleaseComObject(object o);
   public static void ReleaseThreadCache( );
   public static bool SetComObjectData(object obj, object key, object data);
   public static int SizeOf(object structure);
   public static int SizeOf(Type t);
   public static IntPtr StringToBSTR(string s);
   public static IntPtr StringToCoTaskMemAnsi(string s);
   public static IntPtr StringToCoTaskMemAuto(string s);
   public static IntPtr StringToCoTaskMemUni(string s);
   public static IntPtr StringToHGlobalAnsi(string s);
   public static IntPtr StringToHGlobalAuto(string s);
   public static IntPtr StringToHGlobalUni(string s);
   public static void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld);
   public static void ThrowExceptionForHR(int errorCode);
   public static void ThrowExceptionForHR(int errorCode, IntPtr errorInfo);
   public static IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index);
   public static void WriteByte(IntPtr ptr, byte val);
   public static void WriteByte(IntPtr ptr, int ofs, byte val);
   public static void WriteByte(in object ptr, int ofs, byte val);
   public static void WriteInt16(IntPtr ptr, char val);
   public static void WriteInt16(IntPtr ptr, short val);
   public static void WriteInt16(IntPtr ptr, int ofs, char val);
   public static void WriteInt16(IntPtr ptr, int ofs, short val);
   public static void WriteInt16(in object ptr, int ofs, char val);
   public static void WriteInt16(in object ptr, int ofs, short val);
   public static void WriteInt32(IntPtr ptr, int val);
   public static void WriteInt32(IntPtr ptr, int ofs, int val);
   public static void WriteInt32(in object ptr, int ofs, int val);
   public static void WriteInt64(IntPtr ptr, int ofs, long val);
   public static void WriteInt64(IntPtr ptr, long val);
   public static void WriteInt64(in object ptr, int ofs, long val);
   public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val);
   public static void WriteIntPtr(IntPtr ptr, IntPtr val);
   public static void WriteIntPtr(in object ptr, int ofs, IntPtr val);
}


    Part II: Programming with the .NET Framework
    Part IV: API Quick Reference
    Chapter 26. System