NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

dllimport

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
)]

Parameters

dllName
A string specifying the name of the DLL.
entrypoint (Optional)
A string specifying the name of the entry point in the DLL. Default value is the name of the method.
charset (Optional)
A CharSet value specifying the default marshaling of string parameters. Default value is CharSet.Auto.
setlasterror (Optional)
A bool; if true, the method preserves the Win32 "last error" information. Default is false ("last error" is not preserved, but performance is increased).
exactspelling (Optional)
A bool; if true, EntryPoint must exactly match the spelling of the indicated entry point. Default is false, which enables entry point name-matching heuristics.
callingconvention (Optional)
A CallingConvention value specifying the calling convention of the DLL entry point. Default value is CallingConvention.Winapi.

Applies To

Method declarations.

Remarks

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.

Example

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);
    }
}

See Also

C# Attributes | C# Tutorial – Native Code