Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DllImportAttribute.DllImportAttribute Constructor

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

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

public DllImportAttribute (
	string dllName
)
public DllImportAttribute (
	String dllName
)
public function DllImportAttribute (
	dllName : String
)
Not applicable.

Parameters

dllName

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

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.

[DllImport("unmanaged.dll, MyAssembly, Version= 1.0.0.0, Culture=neutral, PublicKeyToken=a77e0ba5eab10125")]

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.

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 Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft