UnmanagedType 枚举

转自:http://msdn.microsoft.com/zh-cn/magazine/system.runtime.interopservices.unmanagedtype(VS.80).aspx

UnmanagedType 枚举

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

命名空间:System.Runtime.InteropServices
程序集:mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration UnmanagedType
Visual Basic(用法)
Dim instance As UnmanagedType
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum UnmanagedType
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum class UnmanagedType
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum UnmanagedType
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public enum UnmanagedType
  成员名称 说明
由 .NET Compact Framework 支持 AnsiBStr 长度前缀为单字节的 ANSI 字符串。可以在 String 数据类型上使用此成员。 
由 .NET Compact Framework 支持 AsAny 一个动态类型,将在运行时确定对象的类型,并将该对象作为所确定的类型进行封送处理。仅对平台调用方法有效。 
由 .NET Compact Framework 支持 Bool 4 字节布尔值(true != 0、false = 0)。这是 Win32 BOOL 类型。 
由 .NET Compact Framework 支持 BStr 长度前缀为双字节的 Unicode 字符串。可以在 String 数据类型上使用此成员(它是 COM 中的默认字符串)。 
由 .NET Compact Framework 支持 ByValArray 当 MarshalAsAttribute.Value 设置为 ByValArray 时,必须设置 SizeConst 以指示数组中的元素数。当需要区分字符串类型时,ArraySubType 字段可以选择包含数组元素的 UnmanagedType。此 UnmanagedType 只可用于作为结构中的字段的数组。 
由 .NET Compact Framework 支持 ByValTStr 用于在结构中出现的内联定长字符数组。与 ByValTStr 一起使用的字符类型由应用于包含结构的 System.Runtime.InteropServices.StructLayoutAttribute 的System.Runtime.InteropServices.CharSet 参数确定。应始终使用MarshalAsAttribute.SizeConst 字段来指示数组的大小。 

.NET Framework 的 ByValTStr 类型的行为类似于结构中的 C 样式、固定大小的字符串(例如,char s[5])。托管代码中的行为与 Microsoft Visual Basic 6.0 中的行为不同,后者不是空终止(例如,MyString As String * 5)。

由 .NET Compact Framework 支持 Currency 在 System.Decimal 上使用,以将十进制数值作为 COM 货币类型而不是 Decimal 封送。 
由 .NET Compact Framework 支持 CustomMarshaler 当与 MarshalAsAttribute.MarshalType 或 MarshalAsAttribute.MarshalTypeRef 一起使用时,指定自定义封送拆收器类。MarshalAsAttribute.MarshalCookie 字段可用于将附加信息传递给自定义封送拆收器。可以在任何引用类型上使用此成员。 
由 .NET Compact Framework 支持 Error 此与 I4 或 U4 关联的本机类型将导致参数作为导出类型库中的 HRESULT 导出。 
由 .NET Compact Framework 支持 FunctionPtr 一个可用作 C 样式函数指针的整数。可将此成员用于 Delegate 数据类型或从Delegate 继承的类型。 
由 .NET Compact Framework 支持 I1 1 字节有符号整数。可使用此成员将布尔值转换为 1 字节、C 样式的 booltrue = 1、false = 0)。 
由 .NET Compact Framework 支持 I2 2 字节有符号整数。 
由 .NET Compact Framework 支持 I4 4 字节有符号整数。 
由 .NET Compact Framework 支持 I8 8 字节有符号整数。 
由 .NET Compact Framework 支持 IDispatch 一个 COM IDispatch 指针(在 Microsoft Visual Basic 6.0 中为 Object)。 
由 .NET Compact Framework 支持 Interface COM 接口指针。从类元数据获得接口的 Guid。如果将此成员应用于类,则可以使用该成员指定确切的接口类型或默认的接口类型。当应用于 Object 数据类型时,此成员将产生 UnmanagedType.IUnknown 行为。 
由 .NET Compact Framework 支持 IUnknown COMIUnknown 指针。可以在 Object 数据类型上使用此成员。 
由 .NET Compact Framework 支持 LPArray 指向 C 样式数组的第一个元素的指针。当从托管到非托管进行封送处理时,该数组的长度由托管数组的长度确定。当从非托管到托管进行封送处理时,将根据MarshalAsAttribute.SizeConst 和 MarshalAsAttribute.SizeParamIndex 字段确定该数组的长度,当需要区分字符串类型时,还可以后跟数组中元素的非托管类型。 
由 .NET Compact Framework 支持 LPStr 单字节、空终止的 ANSI 字符串。可在 System.String 或 System.Text.StringBuilder数据类型上使用此成员。 
由 .NET Compact Framework 支持 LPStruct 一个指针,它指向用于封送托管格式化类的 C 样式结构。仅对平台调用方法有效。 
由 .NET Compact Framework 支持 LPTStr 与平台相关的字符串:在 Windows 98 上为 ANSI,在 Windows NT 和 Windows XP 上为 Unicode。该值仅对平台调用受支持,而对 COM Interop 则不受支持,原因是不支持导出 LPTStr 类型的字符串。 
由 .NET Compact Framework 支持 LPWStr 一个 2 字节、空终止的 Unicode 字符串。 

请注意,如果非托管字符串不是使用非托管的 CoTaskMemAlloc 函数创建的,则不能在此非托管字符串中使用 LPWStr 值。

由 .NET Compact Framework 支持 R4 4 字节浮点数。 
由 .NET Compact Framework 支持 R8 8 字节浮点数。 
由 .NET Compact Framework 支持 SafeArray SafeArray 是自我描述的数组,它带有关联数组数据的类型、秩和界限。可将此成员与 MarshalAsAttribute.SafeArraySubType 字段一起使用,以重写默认元素类型。 
由 .NET Compact Framework 支持 Struct 一个用于封送托管格式化类和值类型的 VARIANT。 
由 .NET Compact Framework 支持 SysInt 与平台相关的有符号整数。在 32 位 Windows 上为 4 字节,在 64 位 Windows 上为 8 字节。 
由 .NET Compact Framework 支持 SysUInt 与平台相关的无符号整数。在 32 位 Windows 上为 4 字节,在 64 位 Windows 上为 8 字节。 
由 .NET Compact Framework 支持 TBStr 一个有长度前缀的与平台相关的 char 字符串。在 Windows 98 上为 ANSI,在 Windows NT 上为 Unicode。很少用到这个类似于 BSTR 的成员。 
由 .NET Compact Framework 支持 U1 1 字节无符号整数。 
由 .NET Compact Framework 支持 U2 2 字节无符号整数。 
由 .NET Compact Framework 支持 U4 4 字节无符号整数。 
由 .NET Compact Framework 支持 U8 8 字节无符号整数。 
由 .NET Compact Framework 支持 VariantBool 2 字节、OLE 定义的 VARIANT_BOOL 类型(true = -1、false = 0)。 
由 .NET Compact Framework 支持 VBByRefStr 允许 Visual Basic 2005 在非托管代码中更改字符串,并将结果在托管代码中反映出来。该值仅对平台调用受支持。 

在 System.Runtime.InteropServices.MarshalAsAttribute 中使用 UnmanagedType 枚举,以指定在与非托管代码进行交互的过程中如何对类型进行封送处理。可以使用此枚举对使用简单值类型(I1、I2、I4、I8、R4、R8、U2、U4 和 U8)、.NET Framework 中未提供的非托管类型以及各种杂项类型的代码进行封送处理。

有关更多信息,请参见 与非托管代码交互操作

下面的代码段演示如何在托管源代码中声明由 COM 组件实现的非托管接口。 System.Runtime.InteropServices.ComImportAttribute 防止 IMyStorage 接口被导回由 COM 使用。(COM 客户端应当直接使用现有 COM 组件。)在此示例中, MarshalAsAttribute 指定若干 UnmanagedType 成员,这些成员表示由原始 COM 接口使用的类型。
Visual Basic
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)> 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
C#
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;
        }
    }
}
C++
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();
   }
};
J#
import System.*;
import 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.
    /** @attribute ComImport()
     */
    /** @attribute Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")
     */
   public interface IMyStorage
   {
        /** @attribute DispId(1)
         */
        Object GetItem(
        /** @attribute In()
            @attribute MarshalAs(UnmanagedType.BStr)
         */String bstrName);
      
      
        /** @attribute DispId(2)
         */
        void GetItems(
        /** @attribute In()
            @attribute MarshalAs(UnmanagedType.BStr)
         */String bstrLocation,
        /** @attribute Out()
            @attribute MarshalAs(UnmanagedType.SafeArray, 
            SafeArraySubType = VarEnum.VT_VARIANT)
         */Object Items[]);
             
        /** @attribute DispId(3)
         */
        void GetItemDescriptions(
            /** @attribute In()
             */String bstrLocation,
            /** @attribute In()
                @attribute Out()
                @attribute MarshalAs(UnmanagedType.SafeArray)
             */Object varDescriptions[]);
               
        /** @attribute DispId(4)
         */
        /** @return MarshalAs(UnmanagedType.VariantBool)
         */
        boolean get_IsEmpty();
    } //IMyStorage

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值