DllImportAttribute クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<AttributeUsageAttribute(AttributeTargets.Method, Inherited:=False)> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class DllImportAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)] [ComVisibleAttribute(true)] public sealed class DllImportAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, Inherited=false)] [ComVisibleAttribute(true)] public ref class DllImportAttribute sealed : public Attribute

DllImportAttribute 属性は、アンマネージ DLL からエクスポートされた関数を呼び出すために必要な情報を提供します。最低限の要件として、エントリ ポイントを含む DLL の名前を指定する必要があります。
この属性は C# および C++ のメソッド定義に対して直接適用します。Visual Basic では、Declare ステートメントが使用された場合にコンパイラによってこの属性が出力されます。BestFitMapping、CallingConvention、ExactSpelling、PreserveSig、SetLastError、ThrowOnUnmappableChar のいずれかのフィールドを含む複雑なメソッド定義の場合は、この属性を Visual Basic のメソッド定義に直接適用します。
メモ : JScript はこの属性をサポートしていません。JScript プログラムからアンマネージ API のメソッドにアクセスするには、C# または Visual Basic のラッパー クラスを使用します。
プラットフォーム呼び出しサービスを使用してアンマネージ DLL の関数にアクセスする方法の詳細については、「アンマネージ DLL 関数の処理」を参照してください。
![]() |
---|

DllImportAttribute 属性を使用して Win32 MessageBox 関数をインポートする方法を次のコード例に示します。このコード例では、インポートしたメソッドを呼び出しています。
Imports System Imports System.Runtime.InteropServices Module Example ' Use DllImport to import the Win32 MessageBox function. <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 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
using System; using System.Runtime.InteropServices; class Example { // Use DllImport to import the Win32 MessageBox function. [DllImport("user32.dll", CharSet = CharSet.Auto)] 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); } }

System.Attribute
System.Runtime.InteropServices.DllImportAttribute


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DllImportAttribute メンバ
System.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention
DllImportAttribute コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


DllImportAttribute 属性を使用して Win32 MessageBox 関数をインポートする方法を次のコード例に示します。このコード例では、インポートしたメソッドを呼び出しています。
Imports System Imports System.Runtime.InteropServices Module Example ' Use DllImport to import the Win32 MessageBox function. <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 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
using System; using System.Runtime.InteropServices; class Example { // Use DllImport to import the Win32 MessageBox function. [DllImport("user32.dll", CharSet = CharSet.Auto)] 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); } }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DllImportAttribute フィールド

名前 | 説明 | |
---|---|---|
![]() | BestFitMapping | Unicode 文字を ANSI 文字に変換するときの、最適マッピング動作のオン/オフを切り替えます。 |
![]() ![]() ![]() ![]() | ExactSpelling | DllImportAttribute.CharSet フィールドで、指定された名前以外のエントリ ポイント名をアンマネージ DLL から共通言語ランタイムに検索させるかどうかを制御します。 |
![]() ![]() ![]() | ThrowOnUnmappableChar | マップできない Unicode 文字 (ANSI の "?" に変換される文字) が見つかったときに、例外をスローするかどうかを指定します。 |

関連項目
DllImportAttribute クラスSystem.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention
DllImportAttribute プロパティ

名前 | 説明 | |
---|---|---|
![]() | TypeId | 派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 ( Attribute から継承されます。) |
![]() | Value | エントリ ポイントを含む DLL ファイルの名前を取得します。 |

関連項目
DllImportAttribute クラスSystem.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention
DllImportAttribute メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

関連項目
DllImportAttribute クラスSystem.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention
DllImportAttribute メンバ
属性付きメソッドがアンマネージ DLL (Dynamic-Link Library) によって静的エントリ ポイントとして公開されることを示します。
DllImportAttribute データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | BestFitMapping | Unicode 文字を ANSI 文字に変換するときの、最適マッピング動作のオン/オフを切り替えます。 |
![]() ![]() ![]() ![]() | ExactSpelling | DllImportAttribute.CharSet フィールドで、指定された名前以外のエントリ ポイント名をアンマネージ DLL から共通言語ランタイムに検索させるかどうかを制御します。 |
![]() ![]() ![]() | ThrowOnUnmappableChar | マップできない Unicode 文字 (ANSI の "?" に変換される文字) が見つかったときに、例外をスローするかどうかを指定します。 |

名前 | 説明 | |
---|---|---|
![]() | TypeId | 派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。) |
![]() | Value | エントリ ポイントを含む DLL ファイルの名前を取得します。 |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

関連項目
DllImportAttribute クラスSystem.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention
- DllImportAttributeのページへのリンク