DllImportAttribute Class


指示非托管动态链接库 (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)]
public sealed class DllImportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class DllImportAttribute : Attribute
type DllImportAttribute = class
    inherit Attribute
Public NotInheritable Class DllImportAttribute
Inherits Attribute


下面的代码示例演示如何使用 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. 对于包含 BestFitMappingCallingConventionExactSpellingPreserveSigSetLastErrorThrowOnUnmappableChar 字段的复杂方法定义,请将此特性直接应用于 Visual Basic 方法定义。For 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.



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



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


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


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


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


控制 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.


指示是否直接转换具有 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 Windows API 函数。Indicates whether the callee calls the SetLastError Windows API function before returning from the attributed method.


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



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

(Inherited from Attribute)

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



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

(Inherited from Attribute)

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

(Inherited from Attribute)

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

(Inherited from Object)

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

(Inherited from Attribute)

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

(Inherited from Attribute)

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

(Inherited from Object)

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

(Inherited from Object)

Explicit Interface Implementations

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

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

(Inherited from 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.

(Inherited from Attribute)

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

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(Inherited from Attribute)

Applies to

See also