DllImportAttribute クラス

定義

属性付きメソッドがアンマネージ ダイナミックリンク ライブラリ (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 、アンマネージ DLL からエクスポートされた関数を呼び出すために必要な情報を提供します。 最小要件として、エントリ ポイントを含む DLL の名前を指定する必要があります。

この属性は、C# および C++ メソッド定義に直接適用します。ただし、 ステートメントを使用すると、Visual Basic コンパイラによってこの属性が Declare 出力されます。 、CallingConventionPreserveSigExactSpellingSetLastErrorまたは ThrowOnUnmappableChar フィールドを含むBestFitMapping複雑なメソッド定義の場合は、この属性を Visual Basic メソッド定義に直接適用します。

メモ JScript では、この属性はサポートされていません。 C# または Visual Basic ラッパー クラスを使用して、JScript プログラムからアンマネージド API メソッドにアクセスできます。

プラットフォーム呼び出しサービスを使用してアンマネージ DLL 内の関数にアクセスする方法の詳細については、「 アンマネージ DLL 関数の使用」を参照してください。

注意

では DllImportAttribute 、ジェネリック型のマーシャリングはサポートされていません。

コンストラクター

DllImportAttribute(String)

DllImportAttribute クラスの新しいインスタンスを、インポートするメソッドを格納した DLL の名前を使用して初期化します。

フィールド

BestFitMapping

Unicode 文字を ANSI 文字に変換するときの、最適マッピング動作のオン/オフを切り替えます。

CallingConvention

エントリ ポイントの呼び出し規約を示します。

CharSet

文字列パラメーターをメソッドにマーシャリングし、名前マングルを制御する方法を示します。

EntryPoint

呼び出す DLL エントリ ポイントの名前または序数を指定します。

ExactSpelling

CharSet フィールドで、指定された名前以外のエントリ ポイント名をアンマネージ DLL から共通言語ランタイムに検索させるかどうかを制御します。

PreserveSig

戻り値を持つ HRESULT アンマネージ メソッドを直接変換するか、戻り値を自動的に例外に変換するかを HRESULT 示します。

SetLastError

呼び出し先が、属性付きメソッドから戻る前に (SetLastError Windows または errno 他のプラットフォームでは) エラーを設定するかどうかを示します。

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)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください