DllImport
attributeThe DllImport
attribute is used to specify the dll location that contains the implementation of an extern method.
[AttributeUsage(AttributeTargets.Method)] public class DllImportAttribute: System.Attribute { public DllImportAttribute(string dllName) {…} public CallingConvention CallingConvention; public CharSet CharSet; public string DllName { get {…} } public string EntryPoint; public bool ExactSpelling; public bool SetLastError; }
Specifically, the DllImport
attribute has the following behaviors:
dllName
parameter that specifies name of the dll in which the imported method can be found. CallingConvention
parameter indicates the calling convention for the entry point. If no CallingConvention
is specified, a default of CallingConvention.WinAPI
is used.CharSet
parameter indicates the character set used in the entry point. If no CharSet
is specified, a default of CharSet.Auto
is used.EntryPoint
parameter gives the name of the entry point in the dll. If no EntryPoint
is specified, then the name of the method itself is used.ExactSpelling
parameter indicates whether EntryPoint
must exactly match the spelling of the indicated entry point. If no ExactSpelling
is specified, a default of false
is used.SetLastError
parameter indicates whether the method preserves the Win32 "last error". If no SetLastError
is specified, a default of false
is used.In addition, a method that is decorated with the DllImport
attribute must have the extern
modifier.