DllImportAttribute.BestFitMapping Field
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.
public: bool BestFitMapping;
public bool BestFitMapping;
val mutable BestFitMapping : bool
Public BestFitMapping As Boolean
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.
[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
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.
Varning
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.
Varning
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.
Produkt | Versioner |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Feedback om .NET
.NET är ett öppen källkod projekt. Välj en länk för att ge feedback: