This documentation is archived and is not being maintained.

DllImportAttribute Class

Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.

For a list of all members of this type, see DllImportAttribute Members.


[Visual Basic]
NotInheritable Public Class DllImportAttribute
   Inherits Attribute
public sealed class DllImportAttribute : Attribute
public __gc __sealed class DllImportAttribute : public Attribute
class DllImportAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


You can apply this attribute to methods.

The DllImportAttribute attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point.

You apply this attribute directly to C# and C++ method definitions; however, the Visual Basic compiler emits this attribute when you use the Declare statement. For complex method definitions that include BestFitMapping, CallingConvention, ExactSpelling, PreserveSig, SetLastError, or ThrowOnUnmappableChar fields, you apply this attribute directly to Visual Basic method definitions.

Note   JScript .NET does not support this attribute. You can use C# or Visual Basic wrapper classes to access unmanaged API methods from JScript .NET programs.

For additional information about using the platform invoke service to access functions in unmanaged DLLs, see Consuming Unmanaged DLL Functions.


[Visual Basic, C#, C++] The following example shows how to apply the DllImportAttribute to a method.

[Visual Basic] 
<DllImport("KERNEL32.DLL", EntryPoint := "MoveFileW", _
   SetLastError := True, CharSet := CharSet.Unicode, _
   ExactSpelling := True, _
   CallingConvention := CallingConvention.StdCall)> _
Public Shared Function MoveFile(src As String, dst As String) As Boolean
    ' Leave function empty - DLLImport attribute forwards calls to MoveFile to
    ' MoveFileW in KERNEL32.DLL.
End Function

[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",  SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
public static extern bool MoveFile(String src, String dst);

 [DllImport(S"KERNEL32.DLL", EntryPoint=S"MoveFileW",  SetLastError=true,
 CharSet=CharSet::Unicode, ExactSpelling=true,
 static bool MoveFile(String* src, String* dst);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

DllImportAttribute Members | System.Runtime.InteropServices Namespace | SetLastError | ExactSpelling | PreserveSig | CallingConvention