Results 1 to 8 of 8

Thread: FYI: UTF-8 + Manifest on Win10.1903 or better

Threaded View

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    FYI: UTF-8 + Manifest on Win10.1903 or better

    Starting with Win10.1903 one can use UTF8 encoded text and display it in a VB textbox as if textbox were unicode compatible.
    https://docs.microsoft.com/en-us/win...utf8-code-page

    Caveats: Manifest required and includes entries for
    - common controls v6 (i.e., theming)
    - <activeCodePage> element with its value set to: UTF-8
    Code:
    <?xml version="1.0" standalone="yes"?>
    <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
    	<assemblyIdentity name="My.Cool.New.Application" version="1.0.0.0" type="win32" processorArchitecture="x86"/>
    	<dependency>
    		<dependentAssembly>
    			<assemblyIdentity name="Microsoft.Windows.Common-Controls" version="6.0.0.0" type="win32" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*"/>
    		</dependentAssembly>
    	</dependency>
    	<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    		<security>
    			<requestedPrivileges>
    				<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
    			</requestedPrivileges>
    		</security>
    	</trustInfo>
    	<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    		<application>
    			<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
    			<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
    			<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
    			<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
    			<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
    		</application>
    	</compatibility>
    	<application xmlns="urn:schemas-microsoft-com:asm.v3">
    		<windowsSettings>
    			<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
    		</windowsSettings>
    	</application>
    </assembly>
    A quick test showed that with/without a BOM, the text appears correctly. In a sample project, I dumped a bunch of unicode text into NotePad and had it save the file as UTF-8. I then simply read the file into a byte array, ensuring the final byte was zero and sent that array to the textbox via SetWindowText API.

    Here's the declaration. Notice that we are using the A, not W unicode, version of the API. Windows added UTF-8 ability for A-version APIs starting with I think Win10.1803
    Code:
    Private Declare Function SetWindowText Lib "user32.dll" Alias "SetWindowTextA" (ByVal hwnd As Long, lpString As Any) As Long
    
       ... read utf-8 file into BYTE array: aData()
        SetWindowText Text1.hwnd, aData(0)
    Note. Trying this without manifesting for common controls failed to display text correctly. Also, trying with common controls and without the new activeCodePage entry failed to display text correctly. I did not test this with theming disabled.

    This is kinda new. Feel free to comment especially regarding gotchas from personal experiences. Not sure how we might use this in the VB world.

    Edited:
    Should be doable with API-created fonts using the character set 65001. However VB stdFont will not accept that character set. stdFont.CharSet is Integer & 65001 converted to integer is -535. Negative values are rejected. Attempts to use COM OleCreateFontIndirect rejects the character set also & resets it to zero. Wouldn't be surprised if that changes down the road. But fonts created with APIs like CreateFont/CreateFontIndirect should be ok.

    Prior to Win10.1903, UTF-8 parsing/conversion still needs to be done using other methods, converting to unicode for use of W-suffix APIs. This manifest entry does not apply to those earlier operating systems.
    Last edited by LaVolpe; Dec 8th, 2019 at 12:46 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width