DllImportAttribute DllImportAttribute DllImportAttribute 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)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DllImportAttribute : Attribute
type DllImportAttribute = class
    inherit Attribute
Public NotInheritable Class DllImportAttribute
Inherits Attribute
继承
DllImportAttributeDllImportAttributeDllImportAttributeDllImportAttribute
属性

示例

下面的代码示例演示如何使用DllImportAttribute属性来导入 Win32MessageBox函数。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
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++方法的定义。但是,Visual Basic 编译器会发出此属性时使用Declare语句。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. 对于复杂的方法定义中包括BestFitMappingCallingConventionExactSpellingPreserveSigSetLastError,或ThrowOnUnmappableChar字段,将此特性应用直接向 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.

构造函数

DllImportAttribute(String) DllImportAttribute(String) DllImportAttribute(String) DllImportAttribute(String)

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

字段

BestFitMapping BestFitMapping BestFitMapping BestFitMapping

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

CallingConvention CallingConvention CallingConvention CallingConvention

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

CharSet CharSet CharSet CharSet

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

EntryPoint EntryPoint EntryPoint EntryPoint

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

ExactSpelling ExactSpelling ExactSpelling 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 PreserveSig PreserveSig 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 SetLastError SetLastError

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

ThrowOnUnmappableChar ThrowOnUnmappableChar ThrowOnUnmappableChar ThrowOnUnmappableChar

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

属性

TypeId TypeId TypeId TypeId

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

(Inherited from Attribute)
Value Value Value Value

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

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

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

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

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

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _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) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _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)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(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) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(Inherited from Attribute)

适用于

另请参阅