UnmanagedType 枚举

定义

指定如何将参数或字段封送到非托管代码。

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
继承
UnmanagedType
属性

字段

AnsiBStr 35

ANSI 字符串是一个带有长度前缀的单字节字符串。 可以在 String 数据类型上使用此成员。

AsAny 40

一个动态类型,将在运行时确定对象的类型,并将该对象作为所确定的类型进行封送处理。 该成员仅对平台调用方法有效。

Bool 2

4 字节布尔值 (true != 0, false = 0)。 这是 Win32 BOOL 类型。

BStr 19

长度前缀为双字节的 Unicode 字符串。 可以在 String 数据类型上使用此成员(它是 COM 中的默认字符串)。

ByValArray 30

Value 属性被设置为 ByValArray 时,必须设置 SizeConst 字段以指示该数组中的元素数。 当需要区分字符串类型时,ArraySubType 字段可以选择包含数组元素的 UnmanagedType。 此 UnmanagedType 只可用于结构中其元素作为字段出现的数组。

ByValTStr 23

用于在结构中出现的内联定长字符数组。 ByValTStr 类型的行为类似于结构中的 C 样式固定大小字符串, (例如 char s[5] ,) 。 与 ByValTStr 一起使用的字符类型由应用于包含结构的 StructLayoutAttribute 属性的 CharSet 参数确定。 应始终使用 SizeConst 字段来指示数组的大小。

Currency 15

货币类型。 在 Decimal 上使用,以将十进制数值作为 COM 货币类型而不是 Decimal 封送。

CustomMarshaler 44

当与 MarshalTypeMarshalTypeRef 字段一起使用时,指定自定义封送拆收器类。 MarshalCookie 字段可用于将附加信息传递给自定义封送拆收器。 可以在任何引用类型上使用此成员。 此成员仅在参数和返回值有效。 不能用于字段。

Error 45

一个本机类型,此类型与 I4U4 关联且将导致参数作为导出类型库中的 HRESULT 导出。

FunctionPtr 38

一个可用作 C 样式函数指针的整数。 可将此成员用于 Delegate 数据类型或从 Delegate 继承的类型。

HString 47

Windows 运行时字符串。 可以在 String 数据类型上使用此成员。 .NET 5 中删除了对 WinRT 的内置支持。 有关解决方法 ,请参阅以前内置支持的类型

I1 3

1 字节有符号整数。 可使用此成员将布尔值转换为 1 字节、C 样式的 bool (true = 1, false = 0)。

I2 5

2 字节有符号整数。

I4 7

4 字节有符号整数。

I8 9

8 字节有符号整数。

IDispatch 26

COM IDispatch 指针(Microsoft Visual Basic 6.0 中的 Object)。

IInspectable 46

Windows 运行时接口指针。 可以在 Object 数据类型上使用此成员。 .NET 5 中删除了对 WinRT 的内置支持

Interface 28

COM 接口指针。 接口的 Guid 可从类元数据获得。 如果将此成员应用于类,则可以使用该成员指定确切的接口类型或默认的接口类型。 应用于 Object 数据类型时,此成员将产生与 IUnknown 相同的行为。

IUnknown 25

COM IUnknown 指针。 可以在 Object 数据类型上使用此成员。

LPArray 42

指向 C 样式数组的第一个元素的指针。 当从托管到非托管代码进行封送处理时,该数组的长度由托管数组的长度确定。 从非托管到托管代码进行封送处理时,将根据 SizeConstSizeParamIndex 字段确定该数组的长度,当需要区分字符串类型时,还可以后跟数组中元素的非托管类型。

LPStr 20

单字节、以 null 结尾的 ANSI 字符串。 可以在 StringStringBuilder 数据类型上使用此成员。

LPStruct 43

一个指针,它指向用于封送托管格式化类的 C 样式结构。 该成员仅对平台调用方法有效。

LPTStr 22

Unicode 字符串。 该值仅支持平台调用而不支持 COM 互操作,因为不支持导出 LPTStr 类型的字符串。

LPUTF8Str 48

指向 UTF-8 编码字符串的指针。

LPWStr 21

一个 2 字节、以 null 结尾的 Unicode 字符串。 不能将 LPWStr 值用于未托管的字符串,除非该字符串使用未托管的 CoTaskMemAlloc 函数创建。

R4 11

4 字节浮点数。

R8 12

8 字节浮点数。

SafeArray 29

SafeArray 是自我描述的数组,它带有关联数组数据的类型、秩和界限。 可将此成员与 SafeArraySubType 字段一起使用,以替代默认元素类型。

Struct 27

一个用于封送托管格式化类和值类型的 VARIANT。

SysInt 31

与平台相关的有符号整数:在 32 位 Windows 上为 4 个字节,在 64 位 Windows 上为 8 个字节。

SysUInt 32

与平台相关的无符号整数:在 32 位 Windows 上为 4 个字节,在 64 位 Windows 上为 8 个字节。

TBStr 36

长度为前缀的 Unicode char 字符串。 很少用到这个类似于 BSTR 的成员。

U1 4

1 字节无符号整数。

U2 6

2 字节无符号整数。

U4 8

4 字节无符号整数。

U8 10

8 字节无符号整数。

VariantBool 37

2 字节、OLE 定义的 VARIANT_BOOL 类型 (true = -1, false = 0)。

VBByRefStr 34

一个值,该值使 Visual Basic 能够更改非托管代码中的字符串,并使结果在托管代码中反映出来。 该值仅支持平台调用。

示例

以下代码片段演示如何在托管源代码中声明由 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中不可用的非托管类型以及各种杂项类型)封送代码。

有关详细信息,请参阅与非托管代码交互操作

适用于

另请参阅