UnmanagedMarshal Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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)

System.Object
  System.Reflection.Emit.UnmanagedMarshal

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

NameDescription
System_CAPS_pubpropertyBaseType

Gets an unmanaged base type. This property is read-only.

System_CAPS_pubpropertyElementCount

Gets a number element. This property is read-only.

System_CAPS_pubpropertyGetUnmanagedType

Indicates an unmanaged type. This property is read-only.

System_CAPS_pubpropertyIIDGuid

Gets a GUID. This property is read-only.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticDefineByValArray(Int32)

Specifies a fixed-length array (ByValArray) to marshal to unmanaged code.

System_CAPS_pubmethodSystem_CAPS_staticDefineByValTStr(Int32)

Specifies a string in a fixed array buffer (ByValTStr) to marshal to unmanaged code.

System_CAPS_pubmethodSystem_CAPS_staticDefineLPArray(UnmanagedType)

Specifies an LPArray to marshal to unmanaged code. The length of an LPArray is determined at runtime by the size of the actual marshaled array.

System_CAPS_pubmethodSystem_CAPS_staticDefineSafeArray(UnmanagedType)

Specifies a SafeArray to marshal to unmanaged code.

System_CAPS_pubmethodSystem_CAPS_staticDefineUnmanagedMarshal(UnmanagedType)

Specifies a given type that is to be marshaled to unmanaged code.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

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 Ildasm.exe (IL Disassembler) 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, _
                AssemblyBuilderAccess.RunAndSave)

        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 = _
            GetType(MarshalAsAttribute).GetConstructor(ciParameters)

        ' 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.
        '
        pb.SetCustomAttribute(cabuilder)


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

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

    End Sub

End Class

.NET Framework
Available since 1.1

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

Return to top
Show: