Mono Class Library: System.Runtime.InteropServices Namespace

System.Runtime.InteropServices.DllImportAttribute Class

Indicates that the target method of this attribute is an export from an unmanaged shared library.

See Also: DllImportAttribute Members

System.Object
     System.Attribute
          System.Runtime.InteropServices.DllImportAttribute

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

Thread Safety

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

Remarks

This attribute provides the information needed to call a method exported from an unmanaged shared library. This attribute provides the name of the shared library file, the name of the method within that library, the calling convention, and character set of the unmanaged function.

Note: A shared library refers to Dynamically Linked Libraries on Windows systems, and Shared Libraries on Unix systems.

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

Example

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.day, 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

Requirements

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