public sealed class DllImportAttribute : Attribute
Object
Attribute
DllImportAttribute
mscorlib
RuntimeInfrastructure
Indicates that the target method of this attribute is an export from an unmanaged shared library.
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.
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
AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false, Inherited=false)
System.Runtime.InteropServices Namespace
DllImportAttribute Constructors
DllImportAttribute Constructor
DllImportAttribute Fields
DllImportAttribute.CallingConvention Field
DllImportAttribute.CharSet Field
DllImportAttribute.EntryPoint Field
DllImportAttribute.ExactSpelling Field
DllImportAttribute Properties
public DllImportAttribute(string dllName);
Constructs and initializes a new instance of the DllImportAttribute class.
- dllName
- A String that specifies the name of the shared library containing the unmanaged method to import.
If the shared library specified in dllName is not found, an error occurs at runtime.
System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace
public CallingConvention CallingConvention;
A CallingConvention value that specifies the calling convention used when passing arguments to the unmanaged implementation of a method in a shared library.
The default CallingConvention value is System.Runtime.InteropServices.CallingConvention.StdCall.
System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace
public CharSet CharSet;
A CharSet value that controls function name modification and indicates how the String arguments to the method will be marshaled.
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.
System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace
public string EntryPoint;
A String that specifies the name of the shared library entry point.
System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace
public bool ExactSpelling;
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.
System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace
public string Value { get; }
Gets the name of the shared library file with the entry point.
A String containing the name of the shared library file from which a function implementation is imported.
This property is read-only.
System.Runtime.InteropServices.DllImportAttribute Class, System.Runtime.InteropServices Namespace