DllImportAttribute DllImportAttribute DllImportAttribute DllImportAttribute Class

定義

指出由 Unmanaged 動態連結程式庫 (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屬性來匯入 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包含、 CallingConventionExactSpellingPreserveSig 、或ThrowOnUnmappableChar欄位的複雜方法定義, 您可以直接將此屬性套用至 Visual Basic 方法定義。 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) 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

指示如何將字串參數封送處理到方法和控制項函式名稱改變 (Name Mangling)。Indicates how to marshal string parameters to the method and controls name mangling.

EntryPoint EntryPoint EntryPoint EntryPoint

指示要呼叫的 DLL 進入點 (Entry Point) 的名稱或序數。Indicates the name or ordinal of the DLL entry point to be called.

ExactSpelling ExactSpelling ExactSpelling ExactSpelling

控制 CharSet 欄位是否會導致 Common Language Runtime 搜尋 Unmanaged 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 傳回值的 Unmanaged 方法,或者是否將 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

在無法對應的 Unicode 字元轉換為 ANSI "?" 字元時,啟用或停用例外狀況的擲回。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 的淺層複本 (Shallow Copy)。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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)

適用於

另請參閱