Redigeeri

Jagamisviis:


DllImportAttribute.ThrowOnUnmappableChar Field

Definition

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 

Field Value

Examples

In some cases, Visual Basic developers use the DllImportAttribute to define a DLL function in managed code, instead of using the Declare statement. Setting the ThrowOnUnmappableChar field is one of those cases. 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,
    BestFitMapping:=False,
    ThrowOnUnmappableChar:=True)>
Friend Shared Function SomeFuncion2(parm As Integer) As Integer
End Function

Remarks

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. This field is false by default.

Caution

Certain Unicode characters are converted to dangerous characters, such as the backslash '\' character, which can inadvertently change a path. By setting the ThrowOnUnmappableChar field to true, you can signal the presence of an unmappable character to the caller by throwing an exception.

Caution

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.

Applies to

See also