UnmanagedType UnmanagedType UnmanagedType UnmanagedType Enum

定義

識別如何封送處理參數或欄位至 Unmanaged 程式碼。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

動態 (Dynamic) 類型,在執行階段判斷物件的類型,並封送處理物件為該類型。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. 唯有當陣列的元素在結構中顯示為欄位時,您才能在該陣列上使用這個 UnmanagedTypeYou can use this UnmanagedType only on an array that whose elements appear as fields in a structure.

ByValTStr ByValTStr ByValTStr ByValTStr 23

使用於出現在結構中的內嵌 (Inline) 固定長度字元陣列。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 貨幣類型,而不是成為 DecimalUsed 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-style 函式指標使用。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-style 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-style 陣列第一個項目的指標。A pointer to the first element of a C-style array. 當從 Managed 程式碼封送處理至 Unmanaged 程式碼時,陣列的長度是由 Managed 陣列的長度來判斷。When marshaling from managed to unmanaged code, the length of the array is determined by the length of the managed array. 從 Unmanaged 程式碼封送處理至 Managed 程式碼時,會從 SizeConstSizeParamIndex 欄位判斷陣列的長度,在需要區別字串類型時,也可以選擇是否要在後面加上陣列中元素的 Unmanaged 類型。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-style 結構的指標,您用來封送處理 Managed 格式化類別。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 on,在 Windows NT 與 Windows XP 上為 Unicode。A platform-dependent character string: ANSI on Windows 98, and Unicode on Windows NT and Windows XP. 因為不支援匯出 LPTStr 類型的字串,所以只有進行平台叫用才支援這個值,若是 COM Interop 則不支援。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,用來封送處理 Managed 格式化類別和實值類型。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 變更 Unmanaged 程式碼中的字串,並且讓結果反映在 Managed 程式碼中。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.

範例

下列程式碼片段示範如何宣告,managed 的程式碼中實作的 COM 元件的 unmanaged 的介面。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屬性來指定如何類型會封送處理期間與 unmanaged 程式碼的互通性。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.

如需詳細資訊,請參閱與 Unmanaged 程式碼互通For more information, see Interoperating with Unmanaged Code.

適用於

另請參閱