System.Runtime.InteropServices.DllImportAttribute Class

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

See Also: DllImportAttribute Members


[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class DllImportAttribute : Attribute


You can apply this attribute to methods.

The System.Runtime.InteropServices.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 DllImportAttribute.BestFitMapping, DllImportAttribute.CallingConvention, DllImportAttribute.ExactSpelling, DllImportAttribute.PreserveSig, DllImportAttribute.SetLastError, or DllImportAttribute.ThrowOnUnmappableChar fields, you apply this attribute directly to Visual Basic method definitions.

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

For additional information about using the platform invoke service to access functions in unmanaged DLLs, see [<topic://cpconconsumingunmanageddllfunctions>].


The System.Runtime.InteropServices.DllImportAttribute does not support marshaling of generic types.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.


The following example demonstrates the use of the System.Runtime.InteropServices.DllImportAttribute.

Note: The non-standard GetLocalTime API used in this example indicates the current local system time.

C# Example

using System;
using System.Runtime.InteropServices;

[ StructLayout( LayoutKind.Sequential )]
public class SystemTime {
 public ushort year; 
 public ushort month;
 public ushort dayOfWeek; 
 public ushort day; 
 public ushort hour; 
 public ushort minute; 
 public ushort second; 
 public ushort milliseconds; 

public class LibWrap {
 [ DllImportAttribute( "Kernel32", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall, EntryPoint="GetLocalTime" )]
 public static extern void GetLocalTime( SystemTime st );

public class DllImportAttributeTest {
 public static void Main() {

 SystemTime st = new SystemTime();
 LibWrap.GetLocalTime( st );
 Console.Write( "The Date and Time is: " );
 Console.Write( "{0:00}/{1:00}/{2} at ", st.month,, st.year );
 Console.WriteLine( "{0:00}:{1:00}:{2:00}", st.hour, st.minute, st.second ); 

When run at the given time on the given date, the output produced was

The Date and Time is: 05/16/2001 at 11:39:17


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