Export (0) Print
Expand All
Expand Minimize

UnmanagedType Enumeration

Identifies how to marshal parameters or fields to unmanaged code.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum class UnmanagedType
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum UnmanagedType
SerializableAttribute 
ComVisibleAttribute(true) 
public enum UnmanagedType

 Member nameDescription
Supported by the .NET Compact FrameworkAnsiBStrAn ANSI character string that is a length prefixed, single byte. You can use this member on the String data type. 
Supported by the .NET Compact FrameworkAsAnyA dynamic type that determines the type of an object at run time and marshals the object as that type. Valid for platform invoke methods only. 
Supported by the .NET Compact FrameworkBoolA 4-byte Boolean value (true != 0, false = 0). This is the Win32 BOOL type. 
Supported by the .NET Compact FrameworkBStrA Unicode character string that is a length-prefixed double byte. You can use this member, which is the default string in COM, on the String data type. 
Supported by the .NET Compact FrameworkByValArrayWhen MarshalAsAttribute.Value is set to ByValArray, the SizeConst must be set to indicate the number of elements in the array. The ArraySubType field can optionally contain the UnmanagedType of the array elements when it is necessary to differentiate among string types. You can only use this UnmanagedType on an array that appear as fields in a structure. 
Supported by the .NET Compact FrameworkByValTStrUsed for in-line, fixed-length character arrays that appear within a structure. The character type used with ByValTStr is determined by the System.Runtime.InteropServices.CharSet argument of the System.Runtime.InteropServices.StructLayoutAttribute applied to the containing structure. Always use the MarshalAsAttribute.SizeConst field to indicate the size of the array. 

.NET Framework ByValTStr types behave like C-style, fixed-size strings inside a structure (for example, char s[5]). The behavior in managed code differs from the Microsoft Visual Basic 6.0 behavior, which is not null terminated (for example, MyString As String * 5).

Supported by the .NET Compact FrameworkCurrencyUsed on a System.Decimal to marshal the decimal value as a COM currency type instead of as a Decimal
Supported by the .NET Compact FrameworkCustomMarshalerSpecifies the custom marshaler class when used with MarshalAsAttribute.MarshalType or MarshalAsAttribute.MarshalTypeRef. The MarshalAsAttribute.MarshalCookie field can be used to pass additional information to the custom marshaler. You can use this member on any reference type. 
Supported by the .NET Compact FrameworkErrorThis native type associated with an I4 or a U4 causes the parameter to be exported as a HRESULT in the exported type library. 
Supported by the .NET Compact FrameworkFunctionPtrAn integer that can be used as a C-style function pointer. You can use this member on a Delegate data type or a type that inherits from a Delegate
Supported by the .NET Compact FrameworkI1A 1-byte signed integer. You can use this member to transform a Boolean value into a 1-byte, C-style bool (true = 1, false = 0). 
Supported by the .NET Compact FrameworkI2A 2-byte signed integer. 
Supported by the .NET Compact FrameworkI4A 4-byte signed integer. 
Supported by the .NET Compact FrameworkI8An 8-byte signed integer. 
Supported by the .NET Compact FrameworkIDispatchA COM IDispatch pointer (Object in Microsoft Visual Basic 6.0). 
Supported by the .NET Compact FrameworkInterfaceA COM interface pointer. The Guid of the interface is obtained from the class metadata. Use this member to specify the exact interface type or the default interface type if you apply it to a class. This member produces UnmanagedType.IUnknown behavior when you apply it to the Object data type. 
Supported by the .NET Compact FrameworkIUnknownA COM IUnknown pointer. You can use this member on the Object data type. 
Supported by the .NET Compact FrameworkLPArrayA pointer to the first element of a C-style array. When marshaling from managed to unmanaged, the length of the array is determined by the length of the managed array. When marshaling from unmanaged to managed, the length of the array is determined from the MarshalAsAttribute.SizeConst and the MarshalAsAttribute.SizeParamIndex fields, optionally followed by the unmanaged type of the elements within the array when it is necessary to differentiate among string types. 
Supported by the .NET Compact FrameworkLPStrA single byte, null-terminated ANSI character string. You can use this member on the System.String or System.Text.StringBuilder data types 
Supported by the .NET Compact FrameworkLPStructA pointer to a C-style structure that you use to marshal managed formatted classes. Valid for platform invoke methods only. 
Supported by the .NET Compact FrameworkLPTStrA platform-dependent character string: ANSI on Windows 98 and Unicode on Windows NT and Windows XP. This value is only supported for platform invoke, and not COM interop, because exporting a string of type LPTStr is not supported. 
Supported by the .NET Compact FrameworkLPWStrA 2-byte, null-terminated Unicode character string. 

Note that you cannot use the LPWStr value with an unmanaged string unless the string was created using the unmanaged CoTaskMemAlloc function.

Supported by the .NET Compact FrameworkR4A 4-byte floating point number. 
Supported by the .NET Compact FrameworkR8An 8-byte floating point number. 
Supported by the .NET Compact FrameworkSafeArrayA SafeArray is a self-describing array that carries the type, rank, and bounds of the associated array data. You can use this member with the MarshalAsAttribute.SafeArraySubType field to override the default element type. 
Supported by the .NET Compact FrameworkStructA VARIANT, which is used to marshal managed formatted classes and value types. 
Supported by the .NET Compact FrameworkSysIntA platform-dependent, signed integer. 4-bytes on 32 bit Windows, 8-bytes on 64 bit Windows. 
Supported by the .NET Compact FrameworkSysUIntA platform-dependent, unsigned integer. 4-bytes on 32 bit Windows, 8-bytes on 64 bit Windows. 
Supported by the .NET Compact FrameworkTBStrA length-prefixed, platform-dependent char string. ANSI on Windows 98, Unicode on Windows NT. You rarely use this BSTR-like member. 
Supported by the .NET Compact FrameworkU1A 1-byte unsigned integer. 
Supported by the .NET Compact FrameworkU2A 2-byte unsigned integer. 
Supported by the .NET Compact FrameworkU4A 4-byte unsigned integer. 
Supported by the .NET Compact FrameworkU8An 8-byte unsigned integer. 
Supported by the .NET Compact FrameworkVariantBoolA 2-byte, OLE-defined VARIANT_BOOL type (true = -1, false = 0). 
Supported by the .NET Compact FrameworkVBByRefStrAllows Visual Basic 2005 to change a string in unmanaged code, and have the results reflected in managed code. This value is only supported for platform invoke. 

Use the UnmanagedType enumeration with the System.Runtime.InteropServices.MarshalAsAttribute to specify how types are marshaled during interoperation with unmanaged code. You can use this enumeration to marshal code using simple value types (I1, I2, I4, I8, R4, R8, U2, U4, and U8), unmanaged types that are unavailable in the .NET Framework, and various miscellaneous types.

For more information, see Interoperating with Unmanaged Code.

The following code fragment demonstrates how to declare in managed source code an unmanaged interface implemented by a COM component. The System.Runtime.InteropServices.ComImportAttribute prevents the IMyStorage interface from being exported back for use by COM. (COM clients should use the existing COM component directly.) In this example, MarshalAsAttribute specifies serveral UnmanagedType members, which represent the types used by the original COM interface.

using namespace System;
using namespace System::Runtime::InteropServices;

// If you do not have a type library for an interface
// you can redeclare it using ComImportAttribute.
// This is how the interface would look in an idl file.
//[
//object,
//uuid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26"),
//dual, helpstring("IMyStorage Interface"),
//pointer_default(unique)
//]
//interface IMyStorage : IDispatch
//{
// [id(1)]
// HRESULT GetItem([in] BSTR bstrName, [out, retval] IDispatch ** ppItem);
// [id(2)]
// HRESULT GetItems([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT)* pItems);
// [id(3)]
// HRESULT GetItemDescriptions([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT) ** ppItems);
// [id(4), propget]
// HRESULT get_IsEmpty([out, retval] BOOL * pfEmpty);
//};
// This is the managed declaration.

[ComImport]
[Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")]
interface class IMyStorage
{
   [DispId(1)]
   Object^ GetItem( [In,MarshalAs(UnmanagedType::BStr)]String^ bstrName );

   //[return : MarshalAs(UnmanagedType::Interface)]

   [DispId(2)]
   void GetItems( [In,MarshalAs(UnmanagedType::BStr)]String^ bstrLocation, [Out,MarshalAs(UnmanagedType::SafeArray,
   SafeArraySubType=VarEnum::VT_VARIANT)]array<Object^>^Items );

   [DispId(3)]
   void GetItemDescriptions( [In]String^ bstrLocation, [In,Out,MarshalAs(UnmanagedType::SafeArray)]array<Object^>^varDescriptions );

   property bool IsEmpty 
   {
      [DispId(4)]
      [returnvalue:MarshalAs(UnmanagedType::VariantBool)]
      bool get();
   }
};

import System.*;
import System.Runtime.InteropServices.*;
   
    // If you do not have a type library for an interface
    // you can redeclare it using ComImportAttribute.
    // This is how the interface would look in an idl file.
    // [
    // object,
    // uuid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26"),
    // dual,    helpstring("IMyStorage Interface"),
    // pointer_default(unique)
    // ]
    // interface IMyStorage : IDispatch
    // {
    //    [id(1)]
    //    HRESULT GetItem([in] BSTR bstrName, [out, retval] IDispatch ** ppItem);
    //    [id(2)]
    //    HRESULT GetItems([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT)* pItems);
    //    [id(3)]
    //    HRESULT GetItemDescriptions([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT) ** ppItems);
    //    [id(4), propget]
    //    HRESULT get_IsEmpty([out, retval] BOOL * pfEmpty);
    // };
    //  This is the managed declaration.
    /** @attribute ComImport()
     */
    /** @attribute Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")
     */
   public interface IMyStorage
   {
        /** @attribute DispId(1)
         */
        Object GetItem(
        /** @attribute In()
            @attribute MarshalAs(UnmanagedType.BStr)
         */String bstrName);
      
      
        /** @attribute DispId(2)
         */
        void GetItems(
        /** @attribute In()
            @attribute MarshalAs(UnmanagedType.BStr)
         */String bstrLocation,
        /** @attribute Out()
            @attribute MarshalAs(UnmanagedType.SafeArray, 
            SafeArraySubType = VarEnum.VT_VARIANT)
         */Object Items[]);
             
        /** @attribute DispId(3)
         */
        void GetItemDescriptions(
            /** @attribute In()
             */String bstrLocation,
            /** @attribute In()
                @attribute Out()
                @attribute MarshalAs(UnmanagedType.SafeArray)
             */Object varDescriptions[]);
               
        /** @attribute DispId(4)
         */
        /** @return MarshalAs(UnmanagedType.VariantBool)
         */
        boolean get_IsEmpty();
    } //IMyStorage

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft