Export (0) Print
Expand All

DllImportAttribute.ThrowOnUnmappableChar Field

Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

public bool ThrowOnUnmappableChar
public boolean ThrowOnUnmappableChar
public var ThrowOnUnmappableChar : boolean

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.

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 Marshaling to provide a close-matching character when no exact match exists. 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. Unmappable characters are usually converted to the default '?' ANSI character.

Caution noteCaution

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 noteCaution

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.

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)]

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft