Charsets and marshaling
string objects, and
System.Text.StringBuilder objects are marshaled depends on the value of the
CharSet field on either the P/Invoke or structure. You can set the
CharSet of a P/Invoke by setting the DllImportAttribute.CharSet field when declaring your P/Invoke. To set the
CharSet for a structure, set the StructLayoutAttribute.CharSet field on your struct declaration. When these attribute fields are not set, it is up to the language compiler to determine which
CharSet to use. C# and VB use the Ansi charset by default.
The following table shows a mapping between each charset and how a character or string is represented when marshaled with that charset:
||Windows||.NET Core 2.2 and earlier on Unix||.NET Core 3.0 and later and Mono on Unix|
Make sure you know what representation your native representation expects when picking your charset.
Send feedback about: