Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the CP_ACP(0) of windows ANSI apis in an application?

I try to draw text using a dll library which has only interfaces of ANSI version encapsulated windows ANSI apis, but I need to store string data using utf-8. I don't want to convert strings using MultiByte/WideChar functions so I want an approach to change the CP_ACP in my application, so that I can input string data into ANSI apis. thanks.

ps: I don't want to change the system default codepage.

like image 501
legendlee Avatar asked Jan 17 '23 16:01

legendlee


2 Answers

CP_ACP represents the system Ansi codepage. You cannot change that on a per-process or per-thread basis. It is a system-wide setting. If the DLL really is dependant on CP_ACP internally, then you have no choice but to convert your from/to UTF-8 whenever you interact with the DLL.

like image 56
Remy Lebeau Avatar answered Jan 27 '23 11:01

Remy Lebeau


Starting with Windows 10 v1903, you can use the application manifest to set the active code page for a given process, which might be different from the system-wide code page :

<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity type="win32" name="..." version="6.0.0.0"/>
  <application>
    <windowsSettings>
      <activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
    </windowsSettings>
  </application>
</assembly>

Obviously, if you need to support older versions of Windows, the application must still be aware that CP_ACP might not be CP_UTF8 and perform any necessary conversions itself.

More details can be found in Microsoft Docs.

like image 22
Daniel Kamil Kozar Avatar answered Jan 27 '23 11:01

Daniel Kamil Kozar