DllImportAttribute.ExactSpelling 字段

定义

控制 CharSet 字段是否使公共语言运行时在非托管 DLL 中搜索入口点名称,而不使用指定的入口点名称。

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

字段值

示例

在某些情况下, Visual Basic 开发人员会使用DllImportAttribute代替 Declare 语句在托管代码中定义 DLL 函数。 设置 ExactSpelling 字段便是这其中的一种情况。

[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

注解

如果false为 ,则当字段设置为 CharSet.AnsiDllImportAttribute.CharSet,将调用附加有字母 A 的入口点名称,当字段设置为 时DllImportAttribute.CharSet,将调用随字母 W 追加的CharSet.Unicode入口点名称。 通常,托管编译器会设置此字段。

下表根据编程语言施加的默认值显示 和 ExactSpelling 字段之间的关系CharSet。 可以替代默认设置,但请谨慎操作。

语言 ANSI Unicode 自动
Visual Basic ExactSpelling:=True ExactSpelling:=True ExactSpelling:=False
C# ExactSpelling=false ExactSpelling=false ExactSpelling=false
C++ ExactSpelling=false ExactSpelling=false ExactSpelling=false

适用于