UnmanagedType 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
标识如何将参数或字段封送给非托管代码。
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
- 继承
- 属性
字段
| 名称 | 值 | 说明 |
|---|---|---|
| Bool | 2 | 一个 4 字节布尔值( |
| I1 | 3 | 1 字节有符号整数。 可以使用此成员将布尔值转换为 1 字节、C 样式 |
| U1 | 4 | 1 字节无符号整数。 |
| I2 | 5 | 2 字节有符号整数。 |
| U2 | 6 | 2 字节无符号整数。 |
| I4 | 7 | 4 字节有符号整数。 |
| U4 | 8 | 4 字节无符号整数。 |
| I8 | 9 | 8 字节有符号整数。 |
| U8 | 10 | 8 字节无符号整数。 |
| R4 | 11 | 4 字节浮点数。 |
| R8 | 12 | 8 字节浮点数。 |
| Currency | 15 | 货币类型。
Decimal用于将十进制值封送为 COM 货币类型,而不是作为 . |
| BStr | 19 | 作为长度前缀的双字节的 Unicode 字符串。 可以在数据类型上使用 String 此成员,该成员是 COM 中的默认字符串。 |
| LPStr | 20 | 单个字节、以 null 结尾的 ANSI 字符串。 可以在数据类型和String数据类型上使用StringBuilder此成员。 |
| LPWStr | 21 | 以 null 结尾的 2 字节 Unicode 字符串。 不能将 |
| LPTStr | 22 | Unicode 字符串。 此值仅支持平台调用,不支持 COM 互操作,因为不支持导出类型 |
| ByValTStr | 23 | 用于在结构中显示的内联固定长度字符数组。
|
| IUnknown | 25 | COM |
| IDispatch | 26 | COM |
| Struct | 27 | VARIANT,用于封送托管格式化类和值类型。 |
| Interface | 28 | COM 接口指针。 接口 Guid 是从类元数据获取的。 如果将其应用于类,请使用此成员指定确切的接口类型或默认接口类型。 此成员生成的行为与 IUnknown 将它应用于 Object 数据类型时的行为相同。 |
| SafeArray | 29 | A |
| ByValArray | 30 |
Value当属性设置为 |
| SysInt | 31 | 依赖于平台的有符号整数:32 位Windows上的 4 个字节,64 位Windows上的 8 个字节。 |
| SysUInt | 32 | 依赖于平台的无符号整数:32 位Windows上的 4 个字节,64 位Windows上的 8 个字节。 |
| VBByRefStr | 34 | 一个值,使 Visual Basic 能够更改非托管代码中的字符串,并让结果反映在托管代码中。 仅平台调用支持此值。 |
| AnsiBStr | 35 | 作为长度前缀的单字节的 ANSI 字符串。 可以在数据类型上使用 String 此成员。 |
| TBStr | 36 | 长度前缀的 Unicode |
| VariantBool | 37 | 一个 2 字节的 OLE 定义的VARIANT_BOOL类型 ( |
| FunctionPtr | 38 | |
| AsAny | 40 | 一种动态类型,用于确定运行时对象的类型,并将对象封送为该类型。 此成员仅适用于平台调用方法。 |
| LPArray | 42 | 指向 C 样式数组的第一个元素的指针。 从托管代码封送到非托管代码时,数组的长度由托管数组的长度决定。 从非托管代码封送到托管代码时,数组的长度取决于 SizeConst 字段, SizeParamIndex 可以选择后跟数组中元素的非托管类型(如果需要区分字符串类型)。 |
| LPStruct | 43 | 指向用于封送托管格式化类的 C 样式结构的指针。 此成员仅适用于平台调用方法。 |
| CustomMarshaler | 44 | 指定与或MarshalType字段一起使用MarshalTypeRef时的自定义封送器类。 该 MarshalCookie 字段可用于将其他信息传递给自定义封送器。 可以在任何引用类型上使用此成员。 此成员对参数有效,并且仅返回值。 不能在字段上使用。 |
| Error | 45 | |
| IInspectable | 46 | Windows 运行时接口指针。 可以在数据类型上使用 Object 此成员。 在 .NET 5 中删除了对 WinRT 的内置支持。 |
| HString | 47 | Windows 运行时字符串。 可以在数据类型上使用 String 此成员。 在 .NET 5 中删除了对 WinRT 的内置支持。 有关解决方法 ,请参阅以前支持的内置类型 。 |
| LPUTF8Str | 48 | 指向 UTF-8 编码字符串的指针。 |
示例
以下代码片段演示如何在托管源代码中声明 COM 组件实现的非托管接口。 该 System.Runtime.InteropServices.ComImportAttribute 属性可防止 IMyStorage 将接口导出回 COM 使用。 (COM 客户端应直接使用现有的 COM 组件。在此示例中, MarshalAsAttribute 指定多个 UnmanagedType 成员,这些成员表示原始 COM 接口使用的类型。
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
注解
在与非托管代码互操作期间,将 UnmanagedType 枚举与属性一起使用 System.Runtime.InteropServices.MarshalAsAttribute ,以指定如何封送类型。 可以使用此枚举使用简单值类型(I1、I2、I4、I8、R4、R8、U2、U4 和 U8)、.NET Framework 中不可用的非托管类型以及各种杂项类型来封送代码。
有关详细信息,请参阅与非托管代码交互操作。