Use UTF-8 code pages in Windows apps - Microsoft Learn

文章推薦指數: 80 %
投票人數:10人

UTF-8 is the universal code page for internationalization and is able to encode the entire Unicode character set. It is used pervasively on the ... Skiptomaincontent Thisbrowserisnolongersupported. UpgradetoMicrosoftEdgetotakeadvantageofthelatestfeatures,securityupdates,andtechnicalsupport. DownloadMicrosoftEdge MoreinfoaboutInternetExplorerandMicrosoftEdge Tableofcontents Exitfocusmode ReadinEnglish Save Tableofcontents ReadinEnglish Save Feedback Edit Print Twitter LinkedIn Facebook Email Tableofcontents UseUTF-8codepagesinWindowsapps Article 04/20/2022 2minutestoread 4contributors Inthisarticle UseUTF-8characterencodingforoptimalcompatibilitybetweenwebappsandother*nix-basedplatforms(Unix,Linux,andvariants),minimizelocalizationbugs,andreducetestingoverhead. UTF-8istheuniversalcodepageforinternationalizationandisabletoencodetheentireUnicodecharacterset.Itisusedpervasivelyontheweb,andisthedefaultfor*nix-basedplatforms. SetaprocesscodepagetoUTF-8 AsofWindowsVersion1903(May2019Update),youcanusetheActiveCodePagepropertyintheappxmanifestforpackagedapps,orthefusionmanifestforunpackagedapps,toforceaprocesstouseUTF-8astheprocesscodepage. Youcandeclarethispropertyandtarget/runonearlierWindowsbuilds,butyoumusthandlelegacycodepagedetectionandconversionasusual.WithaminimumtargetversionofWindowsVersion1903,theprocesscodepagewillalwaysbeUTF-8solegacycodepagedetectionandconversioncanbeavoided. Note Anencodedcharactertakesbetween1and4bytes.UTF-8encodingsupportslongerbytesequences,upto6bytes,butthebiggestcodepointofUnicode6.0(U+10FFFF)onlytakes4bytes. Examples Appxmanifestforapackagedapp: UTF-8 FusionmanifestforanunpackagedWin32app: UTF-8 Note Addamanifesttoanexistingexecutablefromthecommandlinewithmt.exe-manifest-outputresource:;#1 -Avs.-WAPIs Win32APIsoftensupportboth-Aand-Wvariants. -AvariantsrecognizetheANSIcodepageconfiguredonthesystemandsupportchar*,while-WvariantsoperateinUTF-16andsupportWCHAR. Untilrecently,Windowshasemphasized"Unicode"-Wvariantsover-AAPIs.However,recentreleaseshaveusedtheANSIcodepageand-AAPIsasameanstointroduceUTF-8supporttoapps.IftheANSIcodepageisconfiguredforUTF-8,-AAPIstypicallyoperateinUTF-8.Thismodelhasthebenefitofsupportingexistingcodebuiltwith-AAPIswithoutanycodechanges. Codepageconversion AsWindowsoperatesnativelyinUTF-16(WCHAR),youmightneedtoconvertUTF-8datatoUTF-16(orviceversa)tointeroperatewithWindowsAPIs. MultiByteToWideCharandWideCharToMultiByteletyouconvertbetweenUTF-8andUTF-16(WCHAR)(andothercodepages).ThisisparticularlyusefulwhenalegacyWin32APImightonlyunderstandWCHAR.ThesefunctionsallowyoutoconvertUTF-8inputtoWCHARtopassintoa-WAPIandthenconvertanyresultsbackifnecessary. WhenusingthesefunctionswithCodePagesettoCP_UTF8,usedwFlagsofeither0orMB_ERR_INVALID_CHARS,otherwiseanERROR_INVALID_FLAGSoccurs. Note CP_ACPequatestoCP_UTF8onlyifrunningonWindowsVersion1903(May2019Update)oraboveandtheActiveCodePagepropertydescribedaboveissettoUTF-8.Otherwise,ithonorsthelegacysystemcodepage.WerecommendusingCP_UTF8explicitly. Relatedtopics Codepages CodePageIdentifiers Feedback Submitandviewfeedbackfor Thisproduct Thispage Viewallpagefeedback Inthisarticle



請為這篇文章評分?