DllImportAttribute(String) Constructor

Definition

Initializes a new instance of the DllImportAttribute class with the name of the DLL containing the method to import.

C#
public DllImportAttribute(string dllName);

Parameters

dllName
String

The name of the DLL that contains the unmanaged method. In .NET Framework, this can include an assembly display name, if the DLL is included in an assembly.

Examples

The following code example shows how to use the DllImportAttribute attribute to import the Win32 MessageBox function. The code example then calls the imported method.

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

Remarks

.NET Framework only: If an unmanaged DLL file is included in an assembly, for example, by using the linker or the /linkresource compiler option, you can specify the assembly display name as part of dllName. For example, if an unmanaged DLL named unmanaged.dll is included in a managed assembly named MyAssembly, the attribute might be specified as shown in the following code.

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

Applies to

Produkt Verzie
.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