DllImportAttribute.CharSet 字段    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示如何向方法封送字符串参数,并控制名称重整。
public: System::Runtime::InteropServices::CharSet CharSet;public System.Runtime.InteropServices.CharSet CharSet;val mutable CharSet : System.Runtime.InteropServices.CharSetPublic CharSet As CharSet 字段值
示例
下面的代码示例演示如何使用 DllImportAttribute 特性导入 Win32 MessageBox 函数。  然后,该代码示例调用导入的方法。
using System;
using System.Runtime.InteropServices;
class Example
{
    // Use DllImport to import the Win32 MessageBox function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
    
    static void Main()
    {
        // Call the MessageBox function using platform invoke.
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}
Imports System.Runtime.InteropServices
Module Example
    ' Use DllImport to import the Win32 MessageBox function.
    <DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
    Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer
    End Function
    Sub Main()
        ' Call the MessageBox function using platform invoke.
        MessageBox(New IntPtr(0), "Hello World!", "Hello Dialog", 0)
    End Sub
End Module
注解
将此字段与 枚举的成员 CharSet 一起使用可以指定字符串参数的封送处理行为,并指定要调用哪个入口点名称 (给定的确切名称或以“A”或“W”结尾的名称 ) 。 C# 和 Visual Basic 的默认枚举成员为 CharSet.Ansi ,C++ 的默认枚举成员为 CharSet.None,这等效于 CharSet.Ansi。 在 Visual Basic 中,使用 Declare 语句指定 CharSet 字段。
字段 ExactSpelling 会影响字段在确定要调用的入口点名称时的行为 CharSet 。 有关与字段关联的 CharSet 字符串封送处理和名称匹配行为的详细说明和示例,请参阅 指定字符集。