UnmanagedMarshal Class
This documentation is archived and is not being maintained.

UnmanagedMarshal Class

NOTE: This API is now obsolete.

Represents the class that describes how to marshal a field from managed to unmanaged code. This class cannot be inherited.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

<SerializableAttribute> _
<ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")> _
<ComVisibleAttribute(True)> _
<HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort := True)> _
Public NotInheritable Class UnmanagedMarshal
Dim instance As UnmanagedMarshal


The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The code example shows the workaround for this obsolete type.

Marshaling is the process of packaging and unpackaging parameters so remote procedure calls can occur. During marshaling, a field might undergo a format conversion when the format of the managed type is different from the format of the corresponding unmanaged type. For example, you might want to marshal a String type as an unmanaged BSTR. Some format conversions are handled automatically by the runtime. To override the default behavior, you must use the UnmanagedMarshal class to define the format conversion.

The following code example shows replacement code for the obsolete UnmanagedMarshal type. The example emits a single-module assembly named EmitMarshalAs.dll, containing a type named Sample. The type has a method named Test, with one parameter of type String. The code example applies the MarshalAsAttribute with UnmanagedType.BStr to the parameter.

You can use the MSIL Disassembler (Ildasm.exe) to examine the emitted assembly and observe that the parameter is marked marshal(bstr).

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class Example

    Public Shared Sub Main()

        Dim myDomain As AppDomain = AppDomain.CurrentDomain
        Dim myAsmName As New AssemblyName("EmitMarshalAs")

        Dim myAssembly As AssemblyBuilder = _
            myDomain.DefineDynamicAssembly(myAsmName, _

        Dim myModule As ModuleBuilder = _
            myAssembly.DefineDynamicModule(myAsmName.Name, _
                myAsmName.Name & ".dll")

        Dim myType As TypeBuilder = _
            myModule.DefineType("Sample", TypeAttributes.Public)

        Dim myMethod As MethodBuilder = _
            myType.DefineMethod("Test", MethodAttributes.Public, _
                Nothing, new Type() { GetType(String) })

        ' Get a parameter builder for the parameter that needs the  
        ' attribute, using the HasFieldMarshal attribute. In this 
        ' example, the parameter is at position 0 and has the name 
        ' "arg". 
        Dim pb As ParameterBuilder = _
            myMethod.DefineParameter(0, _
               ParameterAttributes.HasFieldMarshal, "arg")

        ' Get the MarshalAsAttribute constructor that takes an 
        ' argument of type UnmanagedType. 
        Dim ciParameters() As Type = { GetType(UnmanagedType) }
        Dim ci As ConstructorInfo = _

        ' Create a CustomAttributeBuilder representing the attribute, 
        ' specifying the necessary unmanaged type. In this case,  
        ' UnmanagedType.BStr is specified. 
        Dim ciArguments() As Object = { UnmanagedType.BStr }
        Dim cabuilder As New CustomAttributeBuilder(ci, ciArguments)

        ' Apply the attribute to the parameter. 

        ' Emit a dummy method body. 
        Dim il As ILGenerator = myMethod.GetILGenerator()

        myAssembly.Save(myAsmName.Name & ".dll")

    End Sub 

End Class


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 1.1, 1.0
Obsolete (compiler warning) in 3.5
Obsolete (compiler warning) in 3.5 SP1
Obsolete (compiler warning) in 3.0
Obsolete (compiler warning) in 3.0 SP1
Obsolete (compiler warning) in 3.0 SP2
Obsolete (compiler warning) in 2.0
Obsolete (compiler warning) in 2.0 SP1
Obsolete (compiler warning) in 2.0 SP2
© 2016 Microsoft