UnmanagedType Enumeration
Collapse the table of content
Expand the table of content

UnmanagedType Enumeration


Identifies how to marshal parameters or fields to unmanaged code.

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

Public Enumeration UnmanagedType

Member nameDescription

An ANSI character string that is a length-prefixed single byte. You can use this member on the String data type.


A dynamic type that determines the type of an object at run time and marshals the object as that type. This member is valid for platform invoke methods only.


A 4-byte Boolean value (true != 0, false = 0). This is the Win32 BOOL type.


A 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.


When the MarshalAsAttribute.Value property is set to ByValArray, the SizeConst field 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 use this UnmanagedType only on an array that whose elements appear as fields in a structure.


Used 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 attribute applied to the containing structure. Always use the MarshalAsAttribute.SizeConst field to indicate the size of the array.


A currency type. Used on a System.Decimal to marshal the decimal value as a COM currency type instead of as a Decimal.


Specifies the custom marshaler class when used with the MarshalAsAttribute.MarshalType or MarshalAsAttribute.MarshalTypeRef field. The MarshalAsAttribute.MarshalCookie field can be used to pass additional information to the custom marshaler. You can use this member on any reference type.


A native type that is associated with an I4 or an U4 and that causes the parameter to be exported as an HRESULT in the exported type library.


An integer that can be used as a C-style function pointer. You can use this member on a Delegate data type or on a type that inherits from a Delegate.


A Windows Runtime string. You can use this member on the System.String data type.


A 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).


A 2-byte signed integer.


A 4-byte signed integer.


An 8-byte signed integer.


A COM IDispatch pointer (Object in Microsoft Visual Basic 6.0).


A Windows Runtime interface pointer. You can use this member on the Object data type.


A 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 the same behavior as UnmanagedType.IUnknown when you apply it to the Object data type.


A COM IUnknown pointer. You can use this member on the Object data type.


A pointer to the first element of a C-style array. When marshaling from managed to unmanaged code, the length of the array is determined by the length of the managed array. When marshaling from unmanaged to managed code, the length of the array is determined from the MarshalAsAttribute.SizeConst and MarshalAsAttribute.SizeParamIndex fields, optionally followed by the unmanaged type of the elements within the array when it is necessary to differentiate among string types.


A single byte, null-terminated ANSI character string. You can use this member on the System.String and System.Text.StringBuilder data types.


A pointer to a C-style structure that you use to marshal managed formatted classes. This member is valid for platform invoke methods only.


A platform-dependent character string: ANSI on Windows 98, and Unicode on Windows NT and Windows XP. This value is supported only for platform invoke and not for COM interop, because exporting a string of type LPTStr is not supported.


A 2-byte, null-terminated Unicode character string.


A 4-byte floating-point number.


An 8-byte floating-point number.


A SafeArray, which 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.


A VARIANT, which is used to marshal managed formatted classes and value types.


A platform-dependent, signed integer: 4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows.


A platform-dependent, unsigned integer: 4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows.


A length-prefixed, platform-dependent char string: ANSI on Windows 98, Unicode on Windows NT. You rarely use this BSTR-like member.


A 1-byte unsigned integer.


A 2-byte unsigned integer.


A 4-byte unsigned integer.


An 8-byte unsigned integer.


A 2-byte, OLE-defined VARIANT_BOOL type (true = -1, false = 0).


A value that enables Visual Basic 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 attribute 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 attribute 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.

Imports System
Imports System.Runtime.InteropServices

Module MyModule
	' 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.

	'dual,	helpstring("IMyStorage Interface"),
	'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")> _
	Public Interface IMyStorage
		<DispId(1)> _
		Function GetItem(<InAttribute(), MarshalAs(UnmanagedType.BStr)> ByVal bstrName As String) _
		   As <MarshalAs(UnmanagedType.Interface)> Object

		<DispId(2)> _
		Function GetItems(<InAttribute(), MarshalAs(UnmanagedType.BStr)> ByVal bstrLocation As String, _
		   <OutAttribute(), MarshalAs(UnmanagedType.SafeArray, SafeArraySubType := VarEnum.VT_VARIANT)> _
                                      ByVal Items() As Object)

		<DispId(3)> _
		Function GetItemDescriptions(<InAttribute()> ByVal bstrLocation As String, _
		   <InAttribute(), OutAttribute(), _
                      MarshalAs(UnmanagedType.SafeArray)> ByRef varDescriptions() As Object)

		<DispId(4)> _
		ReadOnly Property IsEmpty(<MarshalAs(UnmanagedType.VariantBool)> ByVal bEmpty As Boolean)

	End Interface
End Module

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft