英語で読む

次の方法で共有


DllImportAttribute(String) コンストラクター

定義

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

C#
public DllImportAttribute(string dllName);

パラメーター

dllName
String

アンマネージ メソッドを格納する DLL の名前。 .NET Framework では、DLL がアセンブリに含まれている場合、これにはアセンブリの表示名を含めることができます。

次のコード例は、 DllImportAttribute 属性を使用して Win32 MessageBox 関数をインポートする方法を示しています。 次に、このコード例では、インポートされたメソッドを呼び出します。

C#
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);
    }
}

注釈

.NET Framework のみ: たとえば、リンカーまたはコンパイラ オプションを使用して、アンマネージ DLL ファイルがアセンブリに /linkresource 含まれている場合は、 の dllName一部としてアセンブリの表示名を指定できます。 たとえば、 という名前 unmanaged.dll のアンマネージ DLL が という名前のマネージド アセンブリ MyAssemblyに含まれている場合、次のコードに示すように 属性を指定できます。

C#
[DllImport("unmanaged.dll, MyAssembly, Version= 1.0.0.0," +
    "Culture=neutral, PublicKeyToken=a77e0ba5eab10125")]
internal static extern int SomeFuncion1(int parm);

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0