Marshal Class

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.

System.Object
  System.Runtime.InteropServices.Marshal

Namespace:  System.Runtime.InteropServices
Assemblies:   System.Runtime.InteropServices (in System.Runtime.InteropServices.dll)
  mscorlib (in mscorlib.dll)

'Declaration
Public NotInheritable Class Marshal

The Marshal type exposes the following members.

  NameDescription
Public methodStatic memberSupported by Portable Class LibraryAddRefIncrements the reference count on the specified interface.
Public methodStatic memberSupported by Portable Class LibraryAllocCoTaskMemAllocates a block of memory of specified size from the COM task memory allocator.
Public methodStatic memberSupported by Portable Class LibraryAllocHGlobal(Int32)Allocates memory from the unmanaged memory of the process by using the specified number of bytes.
Public methodStatic memberSupported by Portable Class LibraryAllocHGlobal(IntPtr)Allocates memory from the unmanaged memory of the process by using the pointer to the specified number of bytes.
Public methodStatic memberSupported by Portable Class LibraryAreComObjectsAvailableForCleanupIndicates whether runtime callable wrappers (RCWs) from any context are available for cleanup.
Public methodStatic memberSupported by Portable Class LibraryBindToMonikerGets an interface pointer identified by the specified moniker.
Public methodStatic memberChangeWrapperHandleStrengthChanges the strength of an object's COM Callable Wrapper (CCW) handle.
Public methodStatic memberSupported by Portable Class LibraryCleanupUnusedObjectsInCurrentContextNotifies the runtime to clean up all Runtime Callable Wrappers (RCWs) allocated in the current context.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Byte(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Char(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed character array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Double(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed double-precision floating-point number array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Int16(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Int32(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Int64(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Byte(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Char(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed character array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Double(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Int16(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Int32(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Int64(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array.
Public methodStatic memberSupported by Portable Class LibraryCopy(IntPtr, IntPtr(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed IntPtr array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(IntPtr, Single(), Int32, Int32)Copies data from an unmanaged memory pointer to a managed single-precision floating-point number array.
Public methodStatic memberSupported by Portable Class LibraryCopy(IntPtr(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed IntPtr array to an unmanaged memory pointer.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCopy(Single(), Int32, IntPtr, Int32)Copies data from a one-dimensional, managed single-precision floating-point number array to an unmanaged memory pointer.
Public methodStatic memberSupported by Portable Class LibraryCreateAggregatedObject(IntPtr, Object) Obsolete. Aggregates a managed object with the specified COM object.
Public methodStatic memberCreateAggregatedObject(Of T)(IntPtr, T)[Supported in the .NET Framework 4.5.1 and later versions] Aggregates a managed object of the specified type with the specified COM object.
Public methodStatic memberSupported by Portable Class LibraryCreateWrapperOfType(Object, Type) Obsolete. Wraps the specified COM object in an object of the specified type.
Public methodStatic memberCreateWrapperOfType(Of T, TWrapper)(T)[Supported in the .NET Framework 4.5.1 and later versions] Wraps the specified COM object in an object of the specified type.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryDestroyStructure(IntPtr, Type) Obsolete. Frees all substructures that the specified unmanaged memory block points to.
Public methodStatic memberDestroyStructure(Of T)(IntPtr)[Supported in the .NET Framework 4.5.1 and later versions] Frees all substructures of a specified type that the specified unmanaged memory block points to.
Public methodStatic memberSupported by Portable Class LibraryFinalReleaseComObjectReleases all references to a Runtime Callable Wrapper (RCW) by setting its reference count to 0.
Public methodStatic memberSupported by Portable Class LibraryFreeBSTRFrees a BSTR using the COM SysFreeString function.
Public methodStatic memberSupported by Portable Class LibraryFreeCoTaskMemFrees a block of memory allocated by the unmanaged COM task memory allocator.
Public methodStatic memberSupported by Portable Class LibraryFreeHGlobalFrees memory previously allocated from the unmanaged memory of the process.
Public methodStatic memberGenerateGuidForTypeReturns the globally unique identifier (GUID) for the specified type, or generates a GUID using the algorithm used by the Type Library Exporter (Tlbexp.exe).
Public methodStatic memberGenerateProgIdForTypeReturns a programmatic identifier (ProgID) for the specified type.
Public methodStatic memberSupported by Portable Class LibraryGetActiveObjectObtains a running instance of the specified object from the running object table (ROT).
Public methodStatic memberSupported by Portable Class LibraryGetComInterfaceForObject(Object, Type) Obsolete. Returns a pointer to an IUnknown interface that represents the specified interface on the specified object. Custom query interface access is enabled by default.
Public methodStatic memberSupported by Portable Class LibraryGetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) Obsolete. Returns a pointer to an IUnknown interface that represents the specified interface on the specified object. Custom query interface access is controlled by the specified customization mode.
Public methodStatic memberGetComInterfaceForObject(Of T, TInterface)(T)[Supported in the .NET Framework 4.5.1 and later versions] Returns a pointer to an IUnknown interface that represents the specified interface on an object of the specified type. Custom query interface access is enabled by default.
Public methodStatic memberSupported by Portable Class LibraryGetComInterfaceForObjectInContextReturns an interface pointer that represents the specified interface for an object, if the caller is in the same context as that object.
Public methodStatic memberGetComObjectDataRetrieves data that is referenced by the specified key from the specified COM object.
Public methodStatic memberSupported by Portable Class LibraryGetComSlotForMethodInfoRetrieves the virtual function table (v-table or VTBL) slot for a specified System.Reflection.MemberInfo type when that type is exposed to COM.
Public methodStatic memberSupported by Portable Class LibraryGetDelegateForFunctionPointer(IntPtr, Type) Obsolete. Converts an unmanaged function pointer to a delegate.
Public methodStatic memberGetDelegateForFunctionPointer(Of TDelegate)(IntPtr)[Supported in the .NET Framework 4.5.1 and later versions] Converts an unmanaged function pointer to a delegate of a specified type.
Public methodStatic memberSupported by Portable Class LibraryGetEndComSlotRetrieves the last slot in the virtual function table (v-table or VTBL) of a type when exposed to COM.
Public methodStatic memberSupported by Portable Class LibraryGetExceptionCode Obsolete. Retrieves a code that identifies the type of the exception that occurred.
Public methodStatic memberSupported by Portable Class LibraryGetExceptionForHR(Int32)Converts the specified HRESULT error code to a corresponding Exception object.
Public methodStatic memberSupported by Portable Class LibraryGetExceptionForHR(Int32, IntPtr)Converts the specified HRESULT error code to a corresponding Exception object, with additional error information passed in an IErrorInfo interface for the exception object.
Public methodStatic memberSupported by Portable Class LibraryGetExceptionPointersRetrieves a computer-independent description of an exception, and information about the state that existed for the thread when the exception occurred.
Public methodStatic memberSupported by Portable Class LibraryGetFunctionPointerForDelegate(Delegate) Obsolete. Converts a delegate into a function pointer that is callable from unmanaged code.
Public methodStatic memberGetFunctionPointerForDelegate(Of TDelegate)(TDelegate)[Supported in the .NET Framework 4.5.1 and later versions] Converts a delegate of a specified type to a function pointer that is callable from unmanaged code.
Public methodStatic memberGetHINSTANCEReturns the instance handle (HINSTANCE) for the specified module.
Public methodStatic memberSupported by Portable Class LibraryGetHRForExceptionConverts the specified exception to an HRESULT.
Public methodStatic memberSupported by Portable Class LibraryGetHRForLastWin32ErrorReturns the HRESULT corresponding to the last error incurred by Win32 code executed using Marshal.
Public methodStatic memberSupported by Portable Class LibraryGetIDispatchForObjectReturns an IDispatch interface from a managed object.
Public methodStatic memberSupported by Portable Class LibraryGetIDispatchForObjectInContextReturns an IDispatch interface pointer from a managed object, if the caller is in the same context as that object.
Public methodStatic memberGetITypeInfoForTypeReturns a System.Runtime.InteropServices.ComTypes.ITypeInfo interface from a managed type.
Public methodStatic memberSupported by Portable Class LibraryGetIUnknownForObjectReturns an IUnknown interface from a managed object.
Public methodStatic memberSupported by Portable Class LibraryGetIUnknownForObjectInContextReturns an IUnknown interface from a managed object, if the caller is in the same context as that object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGetLastWin32ErrorReturns the error code returned by the last unmanaged function that was called using platform invoke that has the DllImportAttribute.SetLastError flag set.
Public methodStatic memberGetManagedThunkForUnmanagedMethodPtr Obsolete. Gets a pointer to a runtime-generated function that marshals a call from managed to unmanaged code.
Public methodStatic memberSupported by Portable Class LibraryGetMethodInfoForComSlotRetrieves a MemberInfo object for the specified virtual function table (v-table or VTBL) slot.
Public methodStatic memberSupported by Portable Class LibraryGetNativeVariantForObject(Object, IntPtr) Obsolete. Converts an object to a COM VARIANT.
Public methodStatic memberGetNativeVariantForObject(Of T)(T, IntPtr) Obsolete. [Supported in the .NET Framework 4.5.1 and later versions] Converts an object of a specified type to a COM VARIANT.
Public methodStatic memberSupported by Portable Class LibraryGetObjectForIUnknownReturns an instance of a type that represents a COM object by a pointer to its IUnknown interface.
Public methodStatic memberSupported by Portable Class LibraryGetObjectForNativeVariant(IntPtr) Obsolete. Converts a COM VARIANT to an object.
Public methodStatic memberGetObjectForNativeVariant(Of T)(IntPtr) Obsolete. [Supported in the .NET Framework 4.5.1 and later versions] Converts a COM VARIANT to an object of a specified type.
Public methodStatic memberSupported by Portable Class LibraryGetObjectsForNativeVariants(IntPtr, Int32) Obsolete. Converts an array of COM VARIANTs to an array of objects.
Public methodStatic memberGetObjectsForNativeVariants(Of T)(IntPtr, Int32) Obsolete. [Supported in the .NET Framework 4.5.1 and later versions] Converts an array of COM VARIANTs to an array of a specified type.
Public methodStatic memberSupported by Portable Class LibraryGetStartComSlotGets the first slot in the virtual function table (v-table or VTBL) that contains user-defined methods.
Public methodStatic memberGetThreadFromFiberCookie Obsolete. Converts a fiber cookie into the corresponding System.Threading.Thread instance.
Public methodStatic memberGetTypedObjectForIUnknownReturns a managed object of a specified type that represents a COM object.
Public methodStatic memberSupported by Portable Class LibraryGetTypeForITypeInfoConverts an unmanaged ITypeInfo object into a managed System.Type object.
Public methodStatic memberGetTypeFromCLSIDReturns the type associated with the specified class identifier (CLSID).
Public methodStatic memberSupported by Portable Class LibraryGetTypeInfoName(ITypeInfo)Retrieves the name of the type represented by an ITypeInfo object.
Public methodStatic memberGetTypeInfoName(UCOMITypeInfo) Obsolete. Retrieves the name of the type represented by an ITypeInfo object.
Public methodStatic memberGetTypeLibGuid(ITypeLib)Retrieves the library identifier (LIBID) of a type library.
Public methodStatic memberGetTypeLibGuid(UCOMITypeLib) Obsolete. Retrieves the library identifier (LIBID) of a type library.
Public methodStatic memberGetTypeLibGuidForAssemblyRetrieves the library identifier (LIBID) that is assigned to a type library when it was exported from the specified assembly.
Public methodStatic memberGetTypeLibLcid(ITypeLib)Retrieves the LCID of a type library.
Public methodStatic memberGetTypeLibLcid(UCOMITypeLib) Obsolete. Retrieves the LCID of a type library.
Public methodStatic memberGetTypeLibName(ITypeLib)Retrieves the name of a type library.
Public methodStatic memberGetTypeLibName(UCOMITypeLib) Obsolete. Retrieves the name of a type library.
Public methodStatic memberGetTypeLibVersionForAssemblyRetrieves the version number of a type library that will be exported from the specified assembly.
Public methodStatic memberSupported by Portable Class LibraryGetUniqueObjectForIUnknownCreates a unique Runtime Callable Wrapper (RCW) object for a given IUnknown interface.
Public methodStatic memberGetUnmanagedThunkForManagedMethodPtr Obsolete. Gets a pointer to a runtime-generated function that marshals a call from unmanaged to managed code.
Public methodStatic memberSupported by Portable Class LibraryIsComObjectIndicates whether a specified object represents a COM object.
Public methodStatic memberIsTypeVisibleFromComIndicates whether a type is visible to COM clients.
Public methodStatic memberSupported by Portable Class LibraryNumParamBytesCalculates the number of bytes in unmanaged memory that are required to hold the parameters for the specified method.
Public methodStatic memberSupported by Portable Class LibraryOffsetOf(Type, String) Obsolete. Returns the field offset of the unmanaged form of the managed class.
Public methodStatic memberOffsetOf(Of T)(String)[Supported in the .NET Framework 4.5.1 and later versions] Returns the field offset of the unmanaged form of a specified managed class.
Public methodStatic memberSupported by the XNA FrameworkPrelinkExecutes one-time method setup tasks without calling the method.
Public methodStatic memberSupported by the XNA FrameworkPrelinkAllPerforms a pre-link check for all methods on a class.
Public methodStatic memberSupported by Portable Class LibraryPtrToStringAnsi(IntPtr)Copies all characters up to the first null character from an unmanaged ANSI string to a managed String, and widens each ANSI character to Unicode.
Public methodStatic memberSupported by Portable Class LibraryPtrToStringAnsi(IntPtr, Int32)Allocates a managed String, copies a specified number of characters from an unmanaged ANSI string into it, and widens each ANSI character to Unicode.
Public methodStatic memberPtrToStringAuto(IntPtr)Allocates a managed String and copies all characters up to the first null character from a string stored in unmanaged memory into it.
Public methodStatic memberPtrToStringAuto(IntPtr, Int32)Allocates a managed String and copies the specified number of characters from a string stored in unmanaged memory into it.
Public methodStatic memberSupported by Portable Class LibraryPtrToStringBSTRAllocates a managed String and copies a BSTR Data Type string stored in unmanaged memory into it.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryPtrToStringUni(IntPtr)Allocates a managed String and copies all characters up to the first null character from an unmanaged Unicode string into it.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryPtrToStringUni(IntPtr, Int32)Allocates a managed String and copies a specified number of characters from an unmanaged Unicode string into it.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryPtrToStructure(IntPtr, Object) Obsolete. Marshals data from an unmanaged block of memory to a managed object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryPtrToStructure(IntPtr, Type) Obsolete. Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.
Public methodStatic memberPtrToStructure(Of T)(IntPtr)[Supported in the .NET Framework 4.5.1 and later versions] Marshals data from an unmanaged block of memory to a newly allocated managed object of the type specified by a generic type parameter.
Public methodStatic memberPtrToStructure(Of T)(IntPtr, T)[Supported in the .NET Framework 4.5.1 and later versions] Marshals data from an unmanaged block of memory to a managed object of the specified type.
Public methodStatic memberSupported by Portable Class LibraryQueryInterfaceRequests a pointer to a specified interface from a COM object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadByte(IntPtr)Reads a single byte from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadByte(IntPtr, Int32)Reads a single byte at a given offset (or index) from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReadByte(Object, Int32) Obsolete. Reads a single byte at a given offset (or index) from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadInt16(IntPtr)Reads a 16-bit signed integer from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadInt16(IntPtr, Int32)Reads a 16-bit signed integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReadInt16(Object, Int32) Obsolete. Reads a 16-bit signed integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadInt32(IntPtr)Reads a 32-bit signed integer from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadInt32(IntPtr, Int32)Reads a 32-bit signed integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReadInt32(Object, Int32) Obsolete. Reads a 32-bit signed integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadInt64(IntPtr)Reads a 64-bit signed integer from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadInt64(IntPtr, Int32)Reads a 64-bit signed integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReadInt64(Object, Int32) Obsolete. Reads a 64-bit signed integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadIntPtr(IntPtr)Reads a processor native-sized integer from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReadIntPtr(IntPtr, Int32)Reads a processor native sized integer at a given offset from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReadIntPtr(Object, Int32) Obsolete. Reads a processor native sized integer from unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryReAllocCoTaskMemResizes a block of memory previously allocated with AllocCoTaskMem.
Public methodStatic memberSupported by Portable Class LibraryReAllocHGlobalResizes a block of memory previously allocated with AllocHGlobal.
Public methodStatic memberSupported by Portable Class LibraryReleaseDecrements the reference count on the specified interface.
Public methodStatic memberSupported by Portable Class LibraryReleaseComObjectDecrements the reference count of the specified Runtime Callable Wrapper (RCW) associated with the specified COM object.
Public methodStatic memberReleaseThreadCache Obsolete. Releases the thread cache.
Public methodStatic memberSecureStringToBSTRAllocates a BSTR Data Type and copies the contents of a managed SecureString object into it.
Public methodStatic memberSecureStringToCoTaskMemAnsiCopies the contents of a managed SecureString object to a block of memory allocated from the unmanaged COM task allocator.
Public methodStatic memberSecureStringToCoTaskMemUnicodeCopies the contents of a managed SecureString object to a block of memory allocated from the unmanaged COM task allocator.
Public methodStatic memberSecureStringToGlobalAllocAnsiCopies the contents of a managed SecureString into unmanaged memory, converting into ANSI format as it copies.
Public methodStatic memberSecureStringToGlobalAllocUnicodeCopies the contents of a managed SecureString object into unmanaged memory.
Public methodStatic memberSetComObjectDataSets data referenced by the specified key in the specified COM object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySizeOf(Object) Obsolete. Returns the unmanaged size of an object in bytes.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySizeOf(Type) Obsolete. Returns the size of an unmanaged type in bytes.
Public methodStatic memberSizeOf(Of T)[Supported in the .NET Framework 4.5.1 and later versions] Returns the size of an unmanaged type in bytes.
Public methodStatic memberSizeOf(Of T)(T)[Supported in the .NET Framework 4.5.1 and later versions] Returns the unmanaged size of an object of a specified type in bytes.
Public methodStatic memberSupported by Portable Class LibraryStringToBSTRAllocates a BSTR Data Type and copies the contents of a managed String into it.
Public methodStatic memberSupported by Portable Class LibraryStringToCoTaskMemAnsiCopies the contents of a managed String to a block of memory allocated from the unmanaged COM task allocator.
Public methodStatic memberStringToCoTaskMemAutoCopies the contents of a managed String to a block of memory allocated from the unmanaged COM task allocator.
Public methodStatic memberSupported by Portable Class LibraryStringToCoTaskMemUniCopies the contents of a managed String to a block of memory allocated from the unmanaged COM task allocator.
Public methodStatic memberSupported by Portable Class LibraryStringToHGlobalAnsiCopies the contents of a managed String into unmanaged memory, converting into ANSI format as it copies.
Public methodStatic memberStringToHGlobalAutoCopies the contents of a managed String into unmanaged memory, converting into ANSI format if required.
Public methodStatic memberSupported by Portable Class LibraryStringToHGlobalUniCopies the contents of a managed String into unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryStructureToPtr(Object, IntPtr, Boolean) Obsolete. Marshals data from a managed object to an unmanaged block of memory.
Public methodStatic memberStructureToPtr(Of T)(T, IntPtr, Boolean)[Supported in the .NET Framework 4.5.1 and later versions] Marshals data from a managed object of a specified type to an unmanaged block of memory.
Public methodStatic memberSupported by Portable Class LibraryThrowExceptionForHR(Int32)Throws an exception with a specific failure HRESULT value.
Public methodStatic memberSupported by Portable Class LibraryThrowExceptionForHR(Int32, IntPtr)Throws an exception with a specific failure HRESULT, based on the specified IErrorInfo Interface interface.
Public methodStatic memberSupported by Portable Class LibraryUnsafeAddrOfPinnedArrayElement(Array, Int32) Obsolete. Gets the address of the element at the specified index inside the specified array.
Public methodStatic memberUnsafeAddrOfPinnedArrayElement(Of T)(T(), Int32)[Supported in the .NET Framework 4.5.1 and later versions] Gets the address of the element at the specified index in an array of a specified type.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteByte(IntPtr, Byte)Writes a single byte value to unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteByte(IntPtr, Int32, Byte)Writes a single byte value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteByte(Object, Int32, Byte) Obsolete. Writes a single byte value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteInt16(IntPtr, Char)Writes a character as a 16-bit integer value to unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteInt16(IntPtr, Int16)Writes a 16-bit integer value to unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryWriteInt16(IntPtr, Int32, Char)Writes a 16-bit signed integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteInt16(IntPtr, Int32, Int16)Writes a 16-bit signed integer value into unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteInt16(Object, Int32, Char) Obsolete. Writes a 16-bit signed integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteInt16(Object, Int32, Int16) Obsolete. Writes a 16-bit signed integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteInt32(IntPtr, Int32)Writes a 32-bit signed integer value to unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteInt32(IntPtr, Int32, Int32)Writes a 32-bit signed integer value into unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteInt32(Object, Int32, Int32) Obsolete. Writes a 32-bit signed integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteInt64(IntPtr, Int64)Writes a 64-bit signed integer value to unmanaged memory.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteInt64(IntPtr, Int32, Int64)Writes a 64-bit signed integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteInt64(Object, Int32, Int64) Obsolete. Writes a 64-bit signed integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryWriteIntPtr(IntPtr, IntPtr)Writes a processor native sized integer value into unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryWriteIntPtr(IntPtr, Int32, IntPtr)Writes a processor native-sized integer value to unmanaged memory at a specified offset.
Public methodStatic memberSupported by Portable Class LibraryWriteIntPtr(Object, Int32, IntPtr) Obsolete. Writes a processor native sized integer value to unmanaged memory.
Public methodStatic memberSupported by Portable Class LibraryZeroFreeBSTRFrees a BSTR Data Type pointer that was allocated using the SecureStringToBSTR method.
Public methodStatic memberSupported by Portable Class LibraryZeroFreeCoTaskMemAnsiFrees an unmanaged string pointer that was allocated using the SecureStringToCoTaskMemAnsi method.
Public methodStatic memberSupported by Portable Class LibraryZeroFreeCoTaskMemUnicodeFrees an unmanaged string pointer that was allocated using the SecureStringToCoTaskMemUnicode method.
Public methodStatic memberSupported by Portable Class LibraryZeroFreeGlobalAllocAnsiFrees an unmanaged string pointer that was allocated using the SecureStringToGlobalAllocAnsi method.
Public methodStatic memberSupported by Portable Class LibraryZeroFreeGlobalAllocUnicodeFrees an unmanaged string pointer that was allocated using the SecureStringToGlobalAllocUnicode method.
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySystemDefaultCharSizeRepresents the default character size on the system; the default is 2 for Unicode systems and 1 for ANSI systems. This field is read-only.
Public fieldStatic memberSupported by Portable Class LibrarySystemMaxDBCSCharSizeRepresents the maximum size of a double byte character set (DBCS) size, in bytes, for the current operating system. This field is read-only.
Top

The static methods defined on the Marshal class are essential to working with unmanaged code. Most methods defined in this class are typically used by developers who want to provide a bridge between the managed and unmanaged programming models. For example, the StringToHGlobalAnsi method copies 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.

The common language runtime provides specific marshaling capabilities. For details on marshaling behavior, see Interop Marshaling.

The Read and Write methods in the Marshal class support both aligned and unaligned access.

The following example shows how to use various methods defined by the Marshal class.

Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Security.Permissions



Public Structure Point
    Public x, y As Int32
End Structure 



Public NotInheritable Class App

    <SecurityPermission(SecurityAction.LinkDemand, Unrestricted:=True)> _
    Shared Sub 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(GetType(Point)))
        Dim p As 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. 
        Dim hglobal As IntPtr = Marshal.AllocHGlobal(100)
        Marshal.FreeHGlobal(hglobal)
        ' Demonstrate how to use the Marshal class to get the Win32 error  
        ' code when a Win32 method fails. 
        Dim f As [Boolean] = CloseHandle(New IntPtr(-1))
        If Not f Then
            Console.WriteLine("CloseHandle call failed with an error code of: {0}", Marshal.GetLastWin32Error())
        End If 

    End Sub 


    ' 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)> _
    Shared Function CloseHandle(ByVal h As IntPtr) As [Boolean]

    End Function 
End Class 


' 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft