DllImportAttribute.ExactSpelling Field

Definition

Controls whether the CharSet field causes the common language runtime to search an unmanaged DLL for entry-point names other than the one specified.

public: bool ExactSpelling;
public bool ExactSpelling;
val mutable ExactSpelling : bool
Public ExactSpelling As Boolean 

Field Value

Examples

In some cases, Visual Basic developers use the DllImportAttribute, instead of using the Declare statement, to define a DLL function in managed code. Setting the ExactSpelling field is one of those cases.

[DllImport("user32.dll", CharSet = CharSet::Ansi, ExactSpelling = true)]
int MessageBoxA(IntPtr hWnd, String^ Text,
    String^ Caption, unsigned int Type);
internal static class NativeMethods
{
    [DllImport("user32.dll", CharSet = CharSet.Unicode,
        ExactSpelling = true)]
    internal static extern int MessageBoxW(
        IntPtr hWnd, string lpText, string lpCption, uint uType);
}
Friend Class NativeMethods
    <DllImport("user32.dll", ExactSpelling:=False)>
    Friend Shared Function MessageBox(hWnd As IntPtr, lpText As String,
        lpCaption As String, uType As UInteger) As Integer
    End Function
End Class

Remarks

If false, the entry point name appended with the letter A is invoked when the DllImportAttribute.CharSet field is set to CharSet.Ansi, and the entry-point name appended with the letter W is invoked when the DllImportAttribute.CharSet field is set to the CharSet.Unicode. Typically, managed compilers set this field.

The following table shows the relationship between the CharSet and ExactSpelling fields, based on default values imposed by the programming language. You can override the default setting, but do so with caution.

Language ANSI Unicode Auto
Visual Basic ExactSpelling:=True ExactSpelling:=True ExactSpelling:=False
C# ExactSpelling=false ExactSpelling=false ExactSpelling=false
C++ ExactSpelling=false ExactSpelling=false ExactSpelling=false

Applies to