DllImportAttribute 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示由非托管动态链接库 (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 特性导入 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、、CallingConvention、SetLastErrorExactSpellingPreserveSig、 或 ThrowOnUnmappableChar 字段的复杂方法定义,请将此属性直接应用于 Visual Basic 方法定义。
注意 JScript 不支持此属性。 可以使用 C# 或 Visual Basic 包装器类从 JScript 程序访问非托管 API 方法。
有关使用平台调用服务访问非托管 DLL 中的函数的其他信息,请参阅 使用非托管 DLL 函数。
备注
DllImportAttribute不支持泛型类型的封送处理。
Dll |
使用包含要导入的方法的 DLL 的名称初始化 DllImportAttribute 类的新实例。 |
Best |
将 Unicode 字符转换为 ANSI 字符时,启用或禁用最佳映射行为。 |
Calling |
指示入口点的调用约定。 |
Char |
指示如何向方法封送字符串参数,并控制名称重整。 |
Entry |
指示要调用的 DLL 入口点的名称或序号。 |
Exact |
控制 CharSet 字段是否使公共语言运行时在非托管 DLL 中搜索入口点名称,而不使用指定的入口点名称。 |
Preserve |
指示是否直接转换具有 |
Set |
指示在从特性化方法返回之前,被调用方是在 Windows 上还是在 |
Throw |
启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。 |
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
Get |
返回此实例的哈希代码。 (继承自 Attribute) |
Get |
获取当前实例的 Type。 (继承自 Object) |
Is |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
Memberwise |
创建当前 Object 的浅表副本。 (继承自 Object) |
To |
返回表示当前对象的字符串。 (继承自 Object) |
_Attribute. |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute. |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute. |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute. |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |