This documentation is archived and is not being maintained.

DllImportAttribute.BestFitMapping Field

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.

[Visual Basic]
Public BestFitMapping As Boolean
public bool BestFitMapping;
public: bool BestFitMapping;
public var BestFitMapping : Boolean;


If true, best-fit mapping behavior is enabled; otherwise, best-fit mapping is disabled. The BestFitMapping field is true by default. Settings for this field override the any level settings for the System.Runtime.InteropServices.BestFitMappingAttribute attribute.

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. 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   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.


[Visual Basic, C#] In some cases, Visual Basic developers use the DllImportAttribute, instead of the Declare statement, to define a DLL function in managed code. Setting the BestFitMapping 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.

[Visual Basic] 
<DllImport( "My.dll", Charset := Charset.Ansi, _
                      BestfitMapping := False _
                      ThrowOnUnmappableChar := True )>
[DllImport( "My.dll", Charset = Charset.Ansi, 
                      BestfitMapping = false
                      ThrowOnUnmappableChar = true )]

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DllImportAttribute Class | DllImportAttribute Members | System.Runtime.InteropServices Namespace | CharSet | BestFitMappingAttribute