System.Runtime.InteropServices.UnmanagedType Enumeration

Identifies how to marshal parameters or fields to unmanaged code.

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public enum UnmanagedType

Remarks

Use the System.Runtime.InteropServices.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.

Members

Member NameDescription
AnsiBStr

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

AsAny

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.

Bool

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

BStr

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.

ByValArray

When the MarshalAsAttribute.Value property is set to ByValArray, the MarshalAsAttribute.SizeConst field must be set to indicate the number of elements in the array. The MarshalAsAttribute.ArraySubType field can optionally contain the System.Runtime.InteropServices.UnmanagedType of the array elements when it is necessary to differentiate among string types. You can use this System.Runtime.InteropServices.UnmanagedType only on an array that whose elements appear as fields in a structure.

ByValTStr

Used for in-line, fixed-length character arrays that appear within a structure. The character type used with UnmanagedType.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.

Currency

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

CustomMarshaler

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.

Error

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

FunctionPtr

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.

I1

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

I2

A 2-byte signed integer.

I4

A 4-byte signed integer.

I8

An 8-byte signed integer.

IDispatch

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

Interface

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.

IUnknown

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

LPArray

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.

LPStr

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

LPStruct

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

LPTStr

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.

LPWStr

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

R4

A 4-byte floating-point number.

R8

An 8-byte floating-point number.

SafeArray

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.

Struct

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

SysInt

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

SysUInt

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

TBStr

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

U1

A 1-byte unsigned integer.

U2

A 2-byte unsigned integer.

U4

A 4-byte unsigned integer.

U8

An 8-byte unsigned integer.

VariantBool

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

VBByRefStr

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.

Requirements

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0