DllImportAttribute.ExactSpelling 欄位

定義

控制 CharSet 欄位是否會導致 Common Language Runtime 搜尋 Unmanaged DLL 以取得不是指定名稱的進入點名稱。

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

欄位值

範例

在某些情況下,Visual Basic 開發人員會使用 DllImportAttribute ,而不是使用 Declare 語句,在 Managed 程式碼中定義 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.Ansi 時叫用以字母 A 附加的 DllImportAttribute.CharSet 進入點名稱,並在欄位設定 CharSet.Unicode 為 時 DllImportAttribute.CharSet 叫用加上字母 W 的進入點名稱。 一般而言,Managed 編譯器會設定此欄位。

下表根據程式設計語言所加加的預設值,顯示 和 ExactSpelling 欄位之間的 CharSet 關聯性。 您可以覆寫預設設定,但請小心。

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

適用於