UnmanagedType UnmanagedType UnmanagedType UnmanagedType Enum

定义

指定如何将参数或字段封送到非托管代码。Identifies how to marshal parameters or fields to unmanaged code.

public enum class UnmanagedType
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum UnmanagedType
type UnmanagedType = 
Public Enum UnmanagedType
继承
UnmanagedTypeUnmanagedTypeUnmanagedTypeUnmanagedType
属性

字段

AnsiBStr AnsiBStr AnsiBStr AnsiBStr 35

ANSI 字符串是一个带有长度前缀的单字节字符串。An ANSI character string that is a length-prefixed single byte. 可以在 String 数据类型上使用此成员。You can use this member on the String data type.

AsAny AsAny AsAny 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 Bool Bool Bool 2

4 字节布尔值 (true != 0, false = 0)。A 4-byte Boolean value (true != 0, false = 0). 这是 Win32 BOOL 类型。This is the Win32 BOOL type.

BStr BStr BStr BStr 19

长度前缀为双字节的 Unicode 字符串。A Unicode character string that is a length-prefixed double byte. 可以在 String 数据类型上使用此成员(它是 COM 中的默认字符串)。You can use this member, which is the default string in COM, on the String data type.

ByValArray ByValArray ByValArray ByValArray 30

Value 属性被设置为 ByValArray 时,必须设置 SizeConst 字段以指示该数组中的元素数。When the Value property is set to ByValArray, the SizeConst field must be set to indicate the number of elements in the array. 当需要区分字符串类型时,ArraySubType 字段可以选择包含数组元素的 UnmanagedTypeThe ArraySubType field can optionally contain the UnmanagedType of the array elements when it is necessary to differentiate among string types. UnmanagedType 只可用于结构中其元素作为字段出现的数组。You can use this UnmanagedType only on an array that whose elements appear as fields in a structure.

ByValTStr ByValTStr ByValTStr ByValTStr 23

用于在结构中出现的内联定长字符数组。Used for in-line, fixed-length character arrays that appear within a structure. ByValTStr 类型的行为类似于结构内的 C 样式、 固定大小字符串 (例如, char s [5])。ByValTStr types behave like C-style, fixed-size strings inside a structure (for example, char s[5]). ByValTStr 一起使用的字符类型由应用于包含结构的 StructLayoutAttribute 属性的 CharSet 参数确定。The character type used with ByValTStr is determined by the CharSet argument of the StructLayoutAttribute attribute applied to the containing structure. 应始终使用 SizeConst 字段来指示数组的大小。Always use the SizeConst field to indicate the size of the array.

Currency Currency Currency Currency 15

货币类型。A currency type. Decimal 上使用,以将十进制数值作为 COM 货币类型而不是 Decimal 封送。Used on a Decimal to marshal the decimal value as a COM currency type instead of as a Decimal.

CustomMarshaler CustomMarshaler CustomMarshaler CustomMarshaler 44

当与 MarshalTypeMarshalTypeRef 字段一起使用时,指定自定义封送拆收器类。Specifies the custom marshaler class when used with the MarshalType or MarshalTypeRef field. MarshalCookie 字段可用于将附加信息传递给自定义封送拆收器。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 Error Error Error 45

一个本机类型,此类型与 I4U4 关联且将导致参数作为导出类型库中的 HRESULT 导出。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 FunctionPtr FunctionPtr FunctionPtr 38

一个可用作 C 样式函数指针的整数。An integer that can be used as a C-style function pointer. 可将此成员用于 Delegate 数据类型或从 Delegate 继承的类型。You can use this member on a Delegate data type or on a type that inherits from a Delegate.

HString HString HString HString 47

Windows 运行时Windows Runtime 字符串。A Windows 运行时Windows Runtime string. 可以在 String 数据类型上使用此成员。You can use this member on the String data type.

I1 I1 I1 I1 3

1 字节有符号整数。A 1-byte signed integer. 可使用此成员将布尔值转换为 1 字节、C 样式的 bool (true = 1, false = 0)。You can use this member to transform a Boolean value into a 1-byte, C-style bool (true = 1, false = 0).

I2 I2 I2 I2 5

2 字节有符号整数。A 2-byte signed integer.

I4 I4 I4 I4 7

4 字节有符号整数。A 4-byte signed integer.

I8 I8 I8 I8 9

8 字节有符号整数。An 8-byte signed integer.

IDispatch IDispatch IDispatch IDispatch 26

COM IDispatch 指针(Microsoft Visual Basic 6.0 中的 Object)。A COM IDispatch pointer (Object in Microsoft Visual Basic 6.0).

IInspectable IInspectable IInspectable IInspectable 46

Windows 运行时Windows Runtime 接口指针。A Windows 运行时Windows Runtime interface pointer. 可以在 Object 数据类型上使用此成员。You can use this member on the Object data type.

Interface Interface Interface Interface 28

COM 接口指针。A COM interface pointer. 接口的 Guid 可从类元数据获得。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. 应用于 Object 数据类型时,此成员将产生与 IUnknown 相同的行为。This member produces the same behavior as IUnknown when you apply it to the Object data type.

IUnknown IUnknown IUnknown IUnknown 25

COM IUnknown 指针。A COM IUnknown pointer. 可以在 Object 数据类型上使用此成员。You can use this member on the Object data type.

LPArray LPArray LPArray LPArray 42

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

单字节、以 null 结尾的 ANSI 字符串。A single byte, null-terminated ANSI character string. 可以在 StringStringBuilder 数据类型上使用此成员。You can use this member on the String and StringBuilder data types.

LPStruct LPStruct LPStruct LPStruct 43

一个指针,它指向用于封送托管格式化类的 C 样式结构。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 LPTStr LPTStr LPTStr 22

依赖平台的字符字符串:Windows 98 上的 ANSI 以及 Windows NT 和 Windows XP 上的 Unicode。A platform-dependent character string: ANSI on Windows 98, and Unicode on Windows NT and Windows XP. 该值仅支持平台调用而不支持 COM 互操作,因为不支持导出 LPTStr 类型的字符串。This value is supported only for platform invoke and not for COM interop, because exporting a string of type LPTStr is not supported.

LPUTF8Str LPUTF8Str LPUTF8Str LPUTF8Str 48

指向 UTF-8 编码字符串的指针。A pointer to a UTF-8 encoded string.

LPWStr LPWStr LPWStr LPWStr 21

一个 2 字节、以 null 结尾的 Unicode 字符串。A 2-byte, null-terminated Unicode character string. 不能使用LPWStr与非托管字符串值,除非字符串已通过使用非托管CoTaskMemAlloc函数。You cannot use the LPWStr value with an unmanaged string unless the string was created by using the unmanaged CoTaskMemAlloc function.

R4 R4 R4 R4 11

4 字节浮点数。A 4-byte floating-point number.

R8 R8 R8 R8 12

8 字节浮点数。An 8-byte floating-point number.

SafeArray SafeArray SafeArray SafeArray 29

SafeArray 是自我描述的数组,它带有关联数组数据的类型、秩和界限。A SafeArray, which is a self-describing array that carries the type, rank, and bounds of the associated array data. 可将此成员与 SafeArraySubType 字段一起使用,以替代默认元素类型。You can use this member with the SafeArraySubType field to override the default element type.

Struct Struct Struct Struct 27

一个用于封送托管格式化类和值类型的 VARIANT。A VARIANT, which is used to marshal managed formatted classes and value types.

SysInt SysInt SysInt SysInt 31

依赖平台、带符号整数:32 位 Windows 上的 4 个字节、64 位 Windows 上的 8 个字节。A platform-dependent, signed integer: 4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows.

SysUInt SysUInt SysUInt SysUInt 32

依赖平台、不带符号整数:32 位 Windows 上的 4 个字节、64 位 Windows 上的 8 个字节。A platform-dependent, unsigned integer: 4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows.

TBStr TBStr TBStr TBStr 36

带前缀长度、依赖平台的 char 字符串:Windows 98 上的 ANSI、Windows NT 上的 Unicode。A length-prefixed, platform-dependent char string: ANSI on Windows 98, Unicode on Windows NT. 很少用到这个类似于 BSTR 的成员。You rarely use this BSTR-like member.

U1 U1 U1 U1 4

1 字节无符号整数。A 1-byte unsigned integer.

U2 U2 U2 U2 6

2 字节无符号整数。A 2-byte unsigned integer.

U4 U4 U4 U4 8

4 字节无符号整数。A 4-byte unsigned integer.

U8 U8 U8 U8 10

8 字节无符号整数。An 8-byte unsigned integer.

VariantBool VariantBool VariantBool VariantBool 37

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

VBByRefStr VBByRefStr VBByRefStr VBByRefStr 34

一个值,该值使 Visual Basic 能够更改非托管代码中的字符串,并使结果在托管代码中反映出来。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.

示例

下面的代码段演示如何在托管的源代码中声明非托管的 COM 组件实现的接口。The following code fragment demonstrates how to declare, in managed source code, an unmanaged interface implemented by a COM component. System.Runtime.InteropServices.ComImportAttribute属性防止IMyStorage从 com 返回用于导出的接口The System.Runtime.InteropServices.ComImportAttribute attribute prevents the IMyStorage interface from being exported back for use by COM. (COM 客户端应使用现有的 COM 组件直接。)在此示例中,MarshalAsAttribute指定多个UnmanagedType成员,表示由原始 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
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特性以指定在与非托管代码互操作封送类型的方式。Use the UnmanagedType enumeration with the System.Runtime.InteropServices.MarshalAsAttribute attribute to specify how types are marshaled during interoperation with unmanaged code. 此枚举可用于封送代码使用.NET Framework 中不可用的简单值类型 (I1、 I2、 I4、 i8、r8 R4,、 U2、 U4 和 U8),非托管类型和各种其他类型。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.

适用于

另请参阅