DllImportAttribute 类

定义

指示由非托管动态链接库 (DLL) 公开为静态入口点的特性化方法。

public ref class DllImportAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class DllImportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DllImportAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type DllImportAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DllImportAttribute = class
    inherit Attribute
Public NotInheritable Class DllImportAttribute
Inherits Attribute
继承
DllImportAttribute
属性

示例

下面的代码示例演示如何使用 DllImportAttribute 特性导入 Win32 MessageBox 函数。 然后,代码示例调用导入的方法。

using System;
using System.Runtime.InteropServices;

class Example
{
    // Use DllImport to import the Win32 MessageBox function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
    
    static void Main()
    {
        // Call the MessageBox function using platform invoke.
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}
Imports System.Runtime.InteropServices

Module Example

    ' Use DllImport to import the Win32 MessageBox function.
    <DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
    Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer
    End Function


    Sub Main()
        ' Call the MessageBox function using platform invoke.
        MessageBox(New IntPtr(0), "Hello World!", "Hello Dialog", 0)
    End Sub

End Module

注解

可以将此属性应用于方法。

属性 DllImportAttribute 提供调用从非托管 DLL 导出的函数所需的信息。 作为最低要求,必须提供包含入口点的 DLL 的名称。

将此属性直接应用于 C# 和 C++ 方法定义;但是,使用 Declare 语句时,Visual Basic 编译器会发出此属性。 对于包含 BestFitMapping、、CallingConventionExactSpellingPreserveSigSetLastErrorThrowOnUnmappableChar 字段的复杂方法定义,可以将此属性直接应用于 Visual Basic 方法定义。

注意 JScript 不支持此属性。 可以使用 C# 或 Visual Basic 包装类从 JScript 程序访问非托管 API 方法。

有关使用平台调用服务访问非托管 DLL 中的函数的其他信息,请参阅 使用非托管 DLL 函数

注意

DllImportAttribute不支持对泛型类型进行封送处理。

构造函数

DllImportAttribute(String)

使用包含要导入的方法的 DLL 的名称初始化 DllImportAttribute 类的新实例。

字段

BestFitMapping

将 Unicode 字符转换为 ANSI 字符时,启用或禁用最佳映射行为。

CallingConvention

指示入口点的调用约定。

CharSet

指示如何向方法封送字符串参数,并控制名称重整。

EntryPoint

指示要调用的 DLL 入口点的名称或序号。

ExactSpelling

控制 CharSet 字段是否使公共语言运行时在非托管 DLL 中搜索入口点名称,而不使用指定的入口点名称。

PreserveSig

指示是否直接转换具有 HRESULT 返回值的非托管方法,或者是否 HRESULT 自动将返回值转换为异常。

SetLastError

指示被调用方在从特性化方法返回 SetLastError 之前,是在 Windows 或其他 errno 平台上设置错误 () 。

ThrowOnUnmappableChar

启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)
Value

获取包含入口点的 DLL 文件的名称。

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅