語言

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
屬性

欄位

名稱 值 Description
Bool 2

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

I1 3

一個 1 位元組的簽名整數。 你可以利用這個成員將布林值轉換成 1 位元組的 C 格式 bool (true = 1, false = 0)。

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

一種貨幣類型。 用於 a Decimal 上,將十進位值編入 COM 貨幣類型,而非 Decimal。

BStr 19

一個以長度為前綴的雙位元組的 Unicode 字串。 你可以在資料型別中使用這個成員,它是 COM String 的預設字串。

LPStr 20

一個單位元組、零端的 ANSI 字元串。 你可以在 和 String 資料型別中使用這個成員StringBuilder。

LPWStr 21

一個 2 位元組、null 終止的 Unicode 字串。 除非字串是用未管理函式建立的,否則你不能用這個 LPWStr 值來處理未管理 CoTaskMemAlloc 字串。

LPTStr 22

一個 Unicode 字串。 此值僅支援平台調用,不支援 COM 互通,因為不支援匯出型別 LPTStr 的字串。

ByValTStr 23

用於結構中出現的內嵌固定長度字元陣列。 ByValTStr 型別在結構中表現類似 C 風格、固定大小的字串(例如, char s[5])。 所用的 ByValTStr 字元類型由 CharSet 套用到包含結構的屬性參數 StructLayoutAttribute 決定。 一定要用欄位 SizeConst 來表示陣列大小。

IUnknown 25

一個通訊 IUnknown 指標。 你可以在資料型別中使用這個成員 Object 。

IDispatch 26

一個 COM IDispatch 指標(Microsoft Visual Basic 6.0 中的 Object)。

Struct 27

一個變體,用於管理格式化的類別與值型態。

Interface 28

一個 COM 介面指標。 介面的 是 Guid 從類別元資料中取得的。 使用此成員指定精確的介面類型,或是套用到類別時的預設介面類型。 這個元件產生的行為與 IUnknown 你套用到 Object 資料型別時相同。

SafeArray 29

A SafeArray,是一個自描述陣列,攜帶相關陣列資料的型別、秩與界限。 你可以用這個成員搭配欄位 SafeArraySubType 來覆寫預設元素類型。

ByValArray 30

當屬性 Value 設為 ByValArray時, SizeConst 欄位必須設定為表示陣列中元素的數量。 當需要區分字串類型時,欄位ArraySubType可選擇性地包含陣列元素中的 。UnmanagedType 你只能在元素在結構中以欄位形式出現的陣列上使用 UnmanagedType 。

SysInt 31

一個平台依賴的有號整數:32位元 Windows 為 4 位元組,64 位元 Windows 為 8 位元組。

SysUInt 32

一個平台依賴的無符號整數:32位元 Windows 為 4 位元組,64 位元 Windows 為 8 位元組。

VBByRefStr 34

值,可讓 Visual Basic 變更 Unmanaged 程式代碼中的字串,並讓結果反映在 Managed 程式代碼中。 這個數值只支援平台調用。

AnsiBStr 35

一個以長度為前綴的 ANSI 字串。 你可以在資料型別中使用這個成員 String 。

TBStr 36

一個以長度為前綴的 Unicode char 字串。 你很少用這個類似 BSTR 的成員。

VariantBool 37

一個 2 位元組、OLE 定義的 VARIANT_BOOL 型態(true = -1, false = 0)。

FunctionPtr 38

一個整數,可以用作 C 風格的函數指標。 你可以在資料 Delegate 型別或繼承自 的型別 Delegate上使用此成員。

AsAny 40

一個動態型態,在執行時決定物件的型態,並將該物件編組成該型別。 此成員僅適用於平台調用方法。

LPArray 42

指向 C 風格陣列的第一個元素的指標。 當從 managed 程式碼編組到非管理程式碼時,陣列的長度由管理陣列的長度決定。 從非管理程式碼編組到受管理程式碼時,陣列長度由 SizeConst and SizeParamIndex 欄位決定,當需要區分字串類型時,則可選擇後接陣列中元素的非受管理型別。

LPStruct 43

一個指向 C 風格結構的指標,用來管理格式化的類別。 此成員僅適用於平台調用方法。

CustomMarshaler 44

指定與 MarshalType or MarshalTypeRef 欄位搭配使用時的自訂 marshaler 類別。 MarshalCookie該欄位可用來傳遞額外資訊給自訂的指揮官。 你可以用這個成員做任何參考類型。 此成員僅適用於參數與回傳值。 它不能用於田野。

Error 45

一個與 或 I4 相關聯U4的原生型別,該型別會使參數以 HRESULT 形式匯出到匯出的型別庫中。

IInspectable 46

一個 Windows 執行階段 介面指標。 你可以在資料型別中使用這個成員 Object 。 內建對 WinRT 的支援於 .NET 5 中被移除。

HString 47

一個 Windows 執行階段 字串。 你可以在資料型別中使用這個成員 String 。 內建對 WinRT 的支援於 .NET 5 中被移除。 請參見 「過去內建支援型別 」以取得解決方法。

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 中無法使用的非管理型別,以及各種雜項型別。

如需詳細資訊,請參閱與非受控程式碼互通。

適用於

另請參閱