DllImportAttribute 类

定义

指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开。Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.

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

示例

下面的代码示例演示如何使用DllImportAttribute属性导入 Win32 MessageBox函数。The following code example shows how to use the DllImportAttribute attribute to import the Win32 MessageBox function. 然后, 该代码示例调用导入的方法。The code example then calls the imported method.

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

注解

可以将此特性应用于方法。You can apply this attribute to methods.

DllImportAttribute特性提供调用从非托管 DLL 导出的函数所需的信息。The DllImportAttribute attribute provides the information needed to call a function exported from an unmanaged DLL. 至少必须提供包含入口点的 DLL 的名称。As a minimum requirement, you must supply the name of the DLL containing the entry point.

将此特性直接应用于C#和C++方法定义;但是, 在使用Declare语句时, Visual Basic 编译器会发出此特性。You apply this attribute directly to C# and C++ method definitions; however, the Visual Basic compiler emits this attribute when you use the Declare statement. 对于BestFitMapping包括CallingConvention、、 、PreserveSig、或字段的复杂方法定义,将此特性直接应用于VisualBasic方法定义。ThrowOnUnmappableChar ExactSpelling SetLastErrorFor complex method definitions that include BestFitMapping, CallingConvention, ExactSpelling, PreserveSig, SetLastError, or ThrowOnUnmappableChar fields, you apply this attribute directly to Visual Basic method definitions.

注意JScript 不支持此特性。Note JScript does not support this attribute. 可以使用C#或 Visual Basic 包装器类从 JScript 程序访问非托管 API 方法。You can use C# or Visual Basic wrapper classes to access unmanaged API methods from JScript programs.

有关使用平台调用服务访问非托管 Dll 中的函数的其他信息, 请参阅使用非托管 Dll 函数For additional information about using the platform invoke service to access functions in unmanaged DLLs, see Consuming Unmanaged DLL Functions.

备注

DllImportAttribute不支持对泛型类型进行封送处理。The DllImportAttribute does not support marshaling of generic types.

构造函数

DllImportAttribute(String)

使用包含要导入的方法的 DLL 的名称初始化 DllImportAttribute 类的新实例。Initializes a new instance of the DllImportAttribute class with the name of the DLL containing the method to import.

字段

BestFitMapping

将 Unicode 字符转换为 ANSI 字符时,启用或禁用最佳映射行为。Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.

CallingConvention

指示入口点的调用约定。Indicates the calling convention of an entry point.

CharSet

指示如何向方法封送字符串参数,并控制名称重整。Indicates how to marshal string parameters to the method and controls name mangling.

EntryPoint

指示要调用的 DLL 入口点的名称或序号。Indicates the name or ordinal of the DLL entry point to be called.

ExactSpelling

控制 CharSet 字段是否使公共语言运行时在非托管 DLL 中搜索入口点名称,而不使用指定的入口点名称。Controls whether the CharSet field causes the common language runtime to search an unmanaged DLL for entry-point names other than the one specified.

PreserveSig

指示是否直接转换具有 HRESULTretval 返回值的非托管方法,或是否自动将 HRESULTretval 返回值转换为异常。Indicates whether unmanaged methods that have HRESULT or retval return values are directly translated or whether HRESULT or retval return values are automatically converted to exceptions.

SetLastError

指示被调用方在从属性化方法返回之前是否调用 SetLastError Windows API 函数。Indicates whether the callee calls the SetLastError Windows API function before returning from the attributed method.

ThrowOnUnmappableChar

启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)
Value

获取包含入口点的 DLL 文件的名称。Gets the name of the DLL file that contains the entry point.

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

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

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

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

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅