The following guidance is for [DllImport] use and must be manually followed for correct results. .NET 7+ users should consider using [LibraryImport] instead. An in-box Roslyn analyzer (enabled by default) will provide required guidance for the [LibraryImport] attribute in your code.
The way char values, string objects, and System.Text.StringBuilder objects are marshalled 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 type, set the StructLayoutAttribute.CharSet field on your class or struct declaration. When these attribute fields are not set, it is up to the language compiler to determine which CharSet to use. C#, Visual Basic, and F# use the None charset by default, which has the same behavior as the Ansi charset.
If the System.Runtime.InteropServices.DefaultCharSetAttribute is applied on the module in C# or Visual Basic code, then the C# or Visual Basic compiler will emit the provided CharSet by default instead of using CharSet.None. F# does not support the DefaultCharSetAttribute, and always emits CharSet.None by default.
The following table shows a mapping between each charset and how a character or string is represented when marshalled with that charset:
Make sure you know what representation your native representation expects when picking your charset.
Surađujte s nama na GitHubu
Izvor za ovaj sadržaj možete pronaći na GitHubu, gdje možete stvarati i pregledavati probleme i zahtjeve za povlačenjem. Dodatne informacije potražite u našem vodiču za suradnike.
Povratne informacije o proizvodu .NET
.NET je projekt otvorenog koda. Odaberite vezu za slanje povratnih informacija:
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.