UnmanagedType Enum
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Identifies how to marshal parameters or fields to unmanaged code.
public enum class UnmanagedType
public enum UnmanagedType
[System.Serializable]
public enum UnmanagedType
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum UnmanagedType
type UnmanagedType =
[<System.Serializable>]
type UnmanagedType =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type UnmanagedType =
Public Enum UnmanagedType
- Inheritance
- Attributes
Fields
AnsiBStr | 35 | An ANSI character string that is a length-prefixed single byte. You can use this member on the String data type. |
AsAny | 40 | 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 | 2 | A 4-byte Boolean value ( |
BStr | 19 | 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 | 30 | When the Value property is set to |
ByValTStr | 23 | Used for in-line, fixed-length character arrays that appear within a structure. |
Currency | 15 | A currency type. Used on a Decimal to marshal the decimal value as a COM currency type instead of as a |
CustomMarshaler | 44 | Specifies the custom marshaler class when used with the MarshalType or MarshalTypeRef field. The MarshalCookie field can be used to pass additional information to the custom marshaler. You can use this member on any reference type. This member is valid for parameters and return values only. It cannot be used on fields. |
Error | 45 | 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. |
FunctionPtr | 38 | 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. |
HString | 47 | A Windows Runtime string. You can use this member on the String data type. Built-in support for WinRT was removed in .NET 5. See Formerly built-in supported types for workaround. |
I1 | 3 | A 1-byte signed integer. You can use this member to transform a Boolean value into a 1-byte, C-style |
I2 | 5 | A 2-byte signed integer. |
I4 | 7 | A 4-byte signed integer. |
I8 | 9 | An 8-byte signed integer. |
IDispatch | 26 | A COM |
IInspectable | 46 | A Windows Runtime interface pointer. You can use this member on the Object data type. Built-in support for WinRT was removed in .NET 5. |
Interface | 28 | 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 IUnknown when you apply it to the Object data type. |
IUnknown | 25 | A COM |
LPArray | 42 | 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 SizeConst and SizeParamIndex fields, optionally followed by the unmanaged type of the elements within the array when it is necessary to differentiate among string types. |
LPStr | 20 | A single byte, null-terminated ANSI character string. You can use this member on the String and StringBuilder data types. |
LPStruct | 43 | 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 | 22 | A Unicode character string. This value is supported only for platform invoke and not for COM interop, because exporting a string of type |
LPUTF8Str | 48 | A pointer to a UTF-8 encoded string. |
LPWStr | 21 | A 2-byte, null-terminated Unicode character string. You cannot use the |
R4 | 11 | A 4-byte floating-point number. |
R8 | 12 | An 8-byte floating-point number. |
SafeArray | 29 | A |
Struct | 27 | A VARIANT, which is used to marshal managed formatted classes and value types. |
SysInt | 31 | A platform-dependent, signed integer: 4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows. |
SysUInt | 32 | A platform-dependent, unsigned integer: 4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows. |
TBStr | 36 | A length-prefixed, Unicode |
U1 | 4 | A 1-byte unsigned integer. |
U2 | 6 | A 2-byte unsigned integer. |
U4 | 8 | A 4-byte unsigned integer. |
U8 | 10 | An 8-byte unsigned integer. |
VariantBool | 37 | A 2-byte, OLE-defined VARIANT_BOOL type ( |
VBByRefStr | 34 | 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. |
Examples
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 several 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();
}
};
using System;
using System.Runtime.InteropServices;
namespace 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.
//[
//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")]
public interface IMyStorage
{
[DispId(1)]
[return: MarshalAs(UnmanagedType.Interface)]
object GetItem([In, MarshalAs(UnmanagedType.BStr)] String bstrName);
[DispId(2)]
void GetItems([In, MarshalAs(UnmanagedType.BStr)] String bstrLocation,
[Out, MarshalAs( UnmanagedType.SafeArray,
SafeArraySubType = VarEnum.VT_VARIANT )] out Object[] Items);
[DispId(3)]
void GetItemDescriptions([In] String bstrLocation,
[In, Out, MarshalAs(UnmanagedType.SafeArray)] ref Object[] varDescriptions);
bool IsEmpty
{
[DispId(4)]
[return: MarshalAs(UnmanagedType.VariantBool)]
get;
}
}
}
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.
'[
'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")> _
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
Remarks
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.