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.
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] [ObsoleteAttribute(L"An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")] [HostProtectionAttribute(SecurityAction::LinkDemand, MayLeakOnAbort = true)] public ref class UnmanagedMarshal sealed
The UnmanagedMarshal type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | BaseType | Gets an unmanaged base type. This property is read-only. |
![]() | ElementCount | Gets a number element. This property is read-only. |
![]() | GetUnmanagedType | Indicates an unmanaged type. This property is read-only. |
![]() | IIDGuid | Gets a GUID. This property is read-only. |
| Name | Description | |
|---|---|---|
![]() ![]() | DefineByValArray | Specifies a fixed-length array (ByValArray) to marshal to unmanaged code. |
![]() ![]() | DefineByValTStr | Specifies a string in a fixed array buffer (ByValTStr) to marshal to unmanaged code. |
![]() ![]() | DefineLPArray | 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. |
![]() ![]() | DefineSafeArray | Specifies a SafeArray to marshal to unmanaged code. |
![]() ![]() | DefineUnmanagedMarshal | Specifies a given type that is to be marshaled to unmanaged code. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | 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.
Note |
|---|
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 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 (MSIL Disassembler) to examine the emitted assembly and observe that the parameter is marked marshal(bstr).
using namespace System; using namespace System::Reflection; using namespace System::Reflection::Emit; using namespace System::Runtime::InteropServices; void main() { AppDomain^ myDomain = AppDomain::CurrentDomain; AssemblyName^ myAsmName = gcnew AssemblyName("EmitMarshalAs"); AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess::RunAndSave); ModuleBuilder^ myModule = myAssembly->DefineDynamicModule(myAsmName->Name, myAsmName->Name + ".dll"); TypeBuilder^ myType = myModule->DefineType("Sample", TypeAttributes::Public); MethodBuilder^ myMethod = myType->DefineMethod("Test", MethodAttributes::Public, nullptr, gcnew array<Type^> { String::typeid }); // 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". ParameterBuilder^ pb = myMethod->DefineParameter(0, ParameterAttributes::HasFieldMarshal, "arg"); // Get the MarshalAsAttribute constructor that takes an // argument of type UnmanagedType. // //Type^ maattrType = MarshalAsAttribute::typeid; ConstructorInfo^ ci = (MarshalAsAttribute::typeid)->GetConstructor( gcnew array<Type^> { UnmanagedType::typeid }); // Create a CustomAttributeBuilder representing the attribute, // specifying the necessary unmanaged type. In this case, // UnmanagedType.BStr is specified. // CustomAttributeBuilder^ cabuilder = gcnew CustomAttributeBuilder( ci, gcnew array<Object^> { UnmanagedType::BStr }); // Apply the attribute to the parameter. // pb->SetCustomAttribute(cabuilder); ILGenerator^ il = myMethod->GetILGenerator(); il->Emit(OpCodes::Ret); Type^ finished = myType->CreateType(); myAssembly->Save(myAsmName->Name + ".dll"); }
.NET Framework
Supported in: 1.1, 1.0Obsolete (compiler warning) in 4
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
.NET Framework Client Profile
Obsolete (compiler warning) in 4Obsolete (compiler warning) in 3.5 SP1
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
