Identifies a method as being implemented externally by a function exported by an unmanaged DLL.
[dllimport( dllName, EntryPoint=entrypoint, CharSet=charset, SetLastError=setlasterror, ExactSpelling=exactspelling, CallingConvention=callingconvention )]
Method declarations.
dllimport is a single-use attribute. dllimport is an alias for System.InterOp.DllImportAttribute.
The dllimport attribute is used to specify the DLL that contains the implementation of an extern method.
In this example, because ExactSpelling is false (its default), name-matching heuristics will match MessageBox
to either MessageBoxA
or MessageBoxW
, depending on the platform. Strings will automatically be marshaled as ANSI or Unicode as appropriate (because CharSet defaults to CharSet.Auto).
class DllimportExample { [dllimport("user32.dll")] public static extern int MessageBox(int h, string m, string c, int type); public static int Main() { return MessageBox(0, "Hello World!", "Caption", 0); } }
C# Attributes | C# Tutorial – Native Code