DllImportAttribute.ThrowOnUnmappableChar 字段


启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.

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



在某些情况下,Visual Basic 开发人员使用 DllImportAttribute 在托管代码中定义 DLL 函数,而不是使用 Declare 语句。In some cases, Visual Basic developers use the DllImportAttribute to define a DLL function in managed code, instead of using the Declare statement. 设置 ThrowOnUnmappableChar 字段便是这其中的一种情况。Setting the ThrowOnUnmappableChar field is one of those cases. 下面的示例演示如何通过以下方法将最严格的字符映射安全应用到平台调用方法定义: 指定 ANSI 字符集,禁用最佳匹配映射行为,并在未映射的 Unicode 字符上引发异常。The following example shows how to apply the strictest character mapping security to a platform invoke method definitions by specifying the ANSI character set, disabling best fit mapping behavior, and throwing an exception on unmapped Unicode characters.

[DllImport("My.dll", CharSet = CharSet::Ansi,
    BestFitMapping = false,
    ThrowOnUnmappableChar = true)]
int SomeFuncion2(int parm);
[DllImport("My.dll", CharSet = CharSet.Ansi,
    BestFitMapping = false,
    ThrowOnUnmappableChar = true)]
internal static extern int SomeFuncion2(int parm);
<DllImport("My.dll", CharSet:=CharSet.Ansi,
Friend Shared Function SomeFuncion2(parm As Integer) As Integer
End Function


true 指示每次互操作封送拆收器转换不可映射的字符时引发异常;false,指示禁用了 ThrowOnUnmappableChar 字段。true to indicate that an exception is thrown each time the interop marshaler converts an unmappable character; false to indicate that the ThrowOnUnmappableChar field is disabled. 默认情况下,false 此字段。This field is false by default.

公共语言运行时将任何托管的 Unicode 字符转换为 ANSI 字符,这些字符传递给在 Windows 98 或 Windows Me 上执行的非托管方法。The common language runtime converts to ANSI characters any managed Unicode characters passed to an unmanaged method executing on Windows 98 or Windows Me. 最佳映射允许互操作封送拆收器在不存在精确匹配时提供一个匹配的字符。Best-fit mapping enables the interop marshaler to provide a close-matching character when no exact match exists. 例如,封送拆收器将接受 ANSI 字符的非托管方法的 Unicode 版权字符转换为 "c"。For example, the marshaler converts the Unicode copyright character to 'c' for unmanaged methods that accept ANSI characters. 某些字符缺乏最佳表示形式;这些字符称为无法映射。Some characters lack a best-fit representation; these characters are called unmappable. 无法映射的字符通常转换为默认值 "?"ANSI 字符。Unmappable characters are usually converted to the default '?' ANSI character.


某些 Unicode 字符将转换为危险字符,如反斜杠 "\" 字符,这可能会无意中更改路径。Certain Unicode characters are converted to dangerous characters, such as the backslash '\' character, which can inadvertently change a path. 通过将 ThrowOnUnmappableChar 字段设置为 "true,可以通过引发异常来向调用方发出无法映射的字符的信号。By setting the ThrowOnUnmappableChar field to true, you can signal the presence of an unmappable character to the caller by throwing an exception.


BestFitMapping 提供的默认值和 ThrowOnUnmappableChar 字段传递给托管数组(其元素为 ANSI 字符或 LPSTRs 为非托管安全数组)时,不能更改这些默认值。You cannot change the default values provided by the BestFitMapping and ThrowOnUnmappableChar fields when passing a managed array whose elements are ANSI Chars or LPSTRs to an unmanaged safe array. 最佳映射始终处于启用状态,并且不会引发异常。Best-fit mapping is always enabled and no exception is thrown. 请注意,此组合可能会危及安全模型。Be aware that this combination can compromise your security model.