DllImportAttributeとは? わかりやすく解説

DllImportAttribute クラス

属性付きメソッドがアンマネージ DLL (Dynamic-Link Library) によって静的エントリ ポイントとして公開されることを示します。

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.Method, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 DllImportAttribute
    Inherits Attribute
Dim instance As DllImportAttribute
[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
/** @attribute AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)
 */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class DllImportAttribute extends
 Attribute
AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)
 
ComVisibleAttribute(true) 
public final class DllImportAttribute extends
 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 では、ジェネリック型のマーシャリングはサポートしていません。

使用例使用例

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.Object
   System.Attribute
    System.Runtime.InteropServices.DllImportAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DllImportAttribute メンバ
System.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention

DllImportAttribute コンストラクタ

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

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    dllName As String _
)
Dim dllName As String

Dim instance As New DllImportAttribute(dllName)
public DllImportAttribute (
    string dllName
)
public:
DllImportAttribute (
    String^ dllName
)
public DllImportAttribute (
    String dllName
)
public function DllImportAttribute (
    dllName : String
)

パラメータ

dllName

アンマネージ メソッドを格納する 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);
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DllImportAttribute クラス
DllImportAttribute メンバ
System.Runtime.InteropServices 名前空間

DllImportAttribute フィールド


パブリック フィールドパブリック フィールド

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

関連項目

DllImportAttribute クラス
System.Runtime.InteropServices 名前空間
SetLastError
ExactSpelling
PreserveSig
CallingConvention

DllImportAttribute プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

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 データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド DllImportAttribute DllImportAttribute クラスの新しいインスタンスを、インポートするメソッドを格納した DLL の名前を使用して初期化します。
パブリック フィールドパブリック フィールド
  名前 説明
パブリック フィールド BestFitMapping Unicode 文字を ANSI 文字に変換するときの、最適マッピング動作のオン/オフを切り替えます。
パブリック フィールド .NET Compact Framework によるサポート .NET Compact Framework によるサポート .NET Compact Framework によるサポート ExactSpelling DllImportAttribute.CharSet フィールドで、指定された名前以外のエントリ ポイント名をアンマネージ DLL から共通言語ランタイムに検索させるかどうかを制御します。
パブリック フィールド .NET Compact Framework によるサポート .NET Compact Framework によるサポート ThrowOnUnmappableChar マップできない Unicode 文字 (ANSI の "?" に変換される文字) が見つかったときに、例外をスローするかどうかを指定します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド 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」の関連用語

DllImportAttributeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DllImportAttributeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS