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# メソッド定義に直接適用します。ただし、Visual Basic コンパイラは、Declare ステートメントを使用すると、この属性を出力します。 BestFitMappingCallingConventionExactSpellingPreserveSigSetLastError、または ThrowOnUnmappableChar フィールドを含む複雑なメソッド定義の場合は、この属性Visual Basicメソッド定義に直接適用します。 代わりに LibraryImportAttribute 属性を使用することを検討してください。

Note

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

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

Note

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

コンストラクター

名前 説明
DllImportAttribute(String)

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

フィールド

名前 説明
BestFitMapping

Unicode 文字を ANSI 文字に変換するときの最適なマッピング動作を有効または無効にします。

CallingConvention

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

CharSet

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

EntryPoint

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

ExactSpelling

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

PreserveSig

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

SetLastError

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

ThrowOnUnmappableChar

ANSI "?" 文字に変換される、適用できない Unicode 文字に対する例外のスローを有効または無効にします。

プロパティ

名前 説明
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)

適用対象

こちらもご覧ください