System.Runtime.InteropServices.DllImportAttribute Class

public sealed class DllImportAttribute : Attribute

Base Types

Object
  Attribute
    DllImportAttribute

Assembly

mscorlib

Library

RuntimeInfrastructure

Summary

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

Description

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 DllImportAttribute.

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

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

Attributes

AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false, Inherited=false)

See Also

System.Runtime.InteropServices Namespace

Members

DllImportAttribute Constructors

DllImportAttribute Constructor

DllImportAttribute Fields

DllImportAttribute.CallingConvention Field
DllImportAttribute.CharSet Field
DllImportAttribute.EntryPoint Field
DllImportAttribute.ExactSpelling Field

DllImportAttribute Properties

DllImportAttribute.Value Property


DllImportAttribute Constructor

public DllImportAttribute(string dllName);

Summary

Constructs and initializes a new instance of the DllImportAttribute class.

Parameters

dllName
A String that specifies the name of the shared library containing the unmanaged method to import.

Description

If the shared library specified in dllName is not found, an error occurs at runtime.

See Also

System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace

DllImportAttribute.CallingConvention Field

public CallingConvention CallingConvention;

Summary

A CallingConvention value that specifies the calling convention used when passing arguments to the unmanaged implementation of a method in a shared library.

Description

The default CallingConvention value is System.Runtime.InteropServices.CallingConvention.StdCall.

See Also

System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace

DllImportAttribute.CharSet Field

public CharSet CharSet;

Summary

A CharSet value that controls function name modification and indicates how the String arguments to the method will be marshaled.

Description

This field is set to one of the CharSet values to indicate the required modifications to the name of the imported function and to the String arguments of the function. The default value for System.Runtime.InteropServices.DllImportAttribute.CharSet is System.Runtime.InteropServices.CharSet.Ansi.

If System.Runtime.InteropServices.DllImportAttribute.CharSet is set to System.Runtime.InteropServices.CharSet.Unicode, all string arguments are converted to Unicode characters before being passed to the unmanaged implementation. If the field is set to System.Runtime.InteropServices.CharSet.Ansi the string characters are converted to ANSI characters. If System.Runtime.InteropServices.DllImportAttribute.CharSet is set to System.Runtime.InteropServices.CharSet.Auto, the String and function name conversion is platform dependent.

The System.Runtime.InteropServices.DllImportAttribute.CharSet field might also be used to determine which version of a function is imported from the specified shared library by modifying the provided name of the function. The name modification is platform specific, and includes additional characters to indicate the character set.

The default value of this field is System.Runtime.InteropServices.CharSet.Ansi.

See Also

System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace

DllImportAttribute.EntryPoint Field

public string EntryPoint;

Summary

A String that specifies the name of the shared library entry point.

Description

See Also

System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace

DllImportAttribute.ExactSpelling Field

public bool ExactSpelling;

Summary

A Boolean value indicating whether the name of the entry point in the unmanaged library is modified to correspond to the CharSet value specified in the System.Runtime.InteropServices.DllImportAttribute.CharSet field.

Description

See Also

System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace

DllImportAttribute.Value Property

public string Value { get; }

Summary

Gets the name of the shared library file with the entry point.

Property Value

A String containing the name of the shared library file from which a function implementation is imported.

Description

This property is read-only.

See Also

System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace