UnmanagedType 列舉

定義

識別如何封送處理參數或欄位至 Unmanaged 程式碼。

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

動態 (Dynamic) 類型,在執行階段判斷物件的類型,並封送處理物件為該類型。 這個成員僅對平台叫用方法有效。

Bool 2

4 位元組的布林值 (true != 0, false = 0)。 這是 Win32 BOOL 類型。

BStr 19

固定長度雙位元組的 Unicode 字元字串。 您可以在 String 資料類型上使用這個成員,這是 COM 中的預設字串。

ByValArray 30

Value 屬性設為 ByValArray 時,必須設定 SizeConst 欄位以指出陣列中的元素數目。 需要區別字串類型時,ArraySubType 欄位可以選擇性地包含陣列元素的 UnmanagedType。 唯有當陣列的元素在結構中顯示為欄位時,您才能在該陣列上使用這個 UnmanagedType

ByValTStr 23

使用於出現在結構中的內嵌 (Inline) 固定長度字元陣列。 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-style 函式指標使用。 您可以在 Delegate 資料類型或自 Delegate 繼承的類型上使用這個成員。

HString 47

Windows 執行階段字串。 您可以在 String 資料類型上使用這個成員。 .NET 5 已移除 WinRT 的內建支援。 如需因應措施,請參閱 先前內建的支援類型

I1 3

1 位元帶正負號的整數。 您可以使用這個成員,將布林值轉換為 1 位元組、C-style 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-style 陣列第一個項目的指標。 當從 Managed 程式碼封送處理至 Unmanaged 程式碼時,陣列的長度是由 Managed 陣列的長度來判斷。 從 Unmanaged 程式碼封送處理至 Managed 程式碼時,會從 SizeConstSizeParamIndex 欄位判斷陣列的長度,在需要區別字串類型時,也可以選擇是否要在後面加上陣列中元素的 Unmanaged 類型。

LPStr 20

單一位元組、以 Null 結束的 ANSI 字元字串。 您可以在 StringStringBuilder 資料類型上使用這個成員。

LPStruct 43

C-style 結構的指標,您用來封送處理 Managed 格式化類別。 這個成員僅對平台叫用方法有效。

LPTStr 22

Unicode 字元字串。 因為不支援匯出 LPTStr 類型的字串,所以只有進行平台叫用才支援這個值,若是 COM Interop 則不支援。

LPUTF8Str 48

指向以 UTF-8 編碼字串的指標。

LPWStr 21

2 位元組、以 Null 結束的 Unicode 字元字串。 除非您使用非受控的 CoTaskMemAlloc 函式來建立字串,否則就無法搭配使用 LPWStr 值與非受控字串。

R4 11

4 位元組浮點數。

R8 12

8 位元組浮點數。

SafeArray 29

SafeArray,其為自我描述陣列,具有關聯陣列資料的類型、順位及繫結。 您可以將這個成員與 SafeArraySubType 欄位搭配使用,以覆寫預設元素類型。

Struct 27

VARIANT,用來封送處理 Managed 格式化類別和實值類型。

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 變更 Unmanaged 程式碼中的字串,並且讓結果反映在 Managed 程式碼中。 只有平台叫用支援這個值。

範例

下列程式碼片段示範如何在 Managed 原始程式碼中宣告 COM 元件所實作的 Unmanaged 介面。 屬性 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 屬性,以指定在與 Unmanaged 程式碼交互操作期間如何封送處理類型。 您可以使用這個列舉,使用簡單實數值型別封送處理常式代碼, (I1、I2、I4、I8、R4、R8、U2、U4 和 U8) 、.NET Framework中無法使用的 Unmanaged 類型,以及各種其他類型。

如需詳細資訊,請參閱與 Unmanaged 程式碼互通

適用於

另請參閱