DllImportAttribute 類別

定義

表示由 Unmanaged 動態連結程式庫 (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
屬性

範例

下列程式碼範例示範如何使用 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 提供從 Unmanaged DLL 匯出之函式所需的資訊。 作為最低需求,您必須提供包含進入點的 DLL 名稱。

您可以將此屬性直接套用至 C# 和 C++ 方法定義;不過,當您使用 Declare 語句時,Visual Basic 編譯器會發出此屬性。 對於包含 BestFitMappingCallingConventionExactSpellingPreserveSigSetLastErrorThrowOnUnmappableChar 欄位的複雜方法定義,您可以將此屬性直接套用至 Visual Basic 方法定義。

注意 JScript 不支援此屬性。 您可以使用 C# 或 Visual Basic 包裝函式類別,從 JScript 程式存取 Unmanaged API 方法。

如需使用平台叫用服務存取 Unmanaged DLL 中函式的其他資訊,請參閱 取用非受控 DLL 函式

注意

DllImportAttribute不支援泛型型別的封送處理。

建構函式

DllImportAttribute(String)

使用含有要匯入方法的 DLL 名稱,來初始化 DllImportAttribute 類別的新執行個體。

欄位

BestFitMapping

將 Unicode 字元轉換成 ANSI 字元時,啟用或停用最適合對應行為。

CallingConvention

指示進入點的呼叫慣例。

CharSet

指示如何將字串參數封送處理到方法和控制項函式名稱改變 (Name Mangling)。

EntryPoint

指示要呼叫的 DLL 進入點 (Entry Point) 的名稱或序數。

ExactSpelling

控制 CharSet 欄位是否會導致 Common Language Runtime 搜尋 Unmanaged DLL 以取得不是指定名稱的進入點名稱。

PreserveSig

指出是否直接轉譯具有 HRESULT 傳回值的 Unmanaged 方法,或傳回值是否 HRESULT 自動轉換成例外狀況。

SetLastError

指出被呼叫者是否在 Windows 或其他 errno 平臺上設定錯誤 () SetLastError ,然後再從屬性化方法傳回。

ThrowOnUnmappableChar

在無法對應的 Unicode 字元轉換為 ANSI "?" 字元時,啟用或停用例外狀況的擲回。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得包含進入點的 DLL 檔案名稱。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱