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 クラスのページへのリンク