Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows API ANSI functions and UTF-8

Tags:

Is it possible to use Windows API ANSI functions with UTF-8 strings?

For example, say I have a path encoded in UTF-8. Can I call CreateDirectoryA or CreateFileA and use a UTF-8 path, or do I have to perform some conversion before calling the functions?

like image 990
krebstar Avatar asked Jan 12 '12 06:01

krebstar


People also ask

What are Windows API functions?

The Windows API (application programming interface) allows user-written programs to interact with Windows, for example to display things on screen and get input from mouse and keyboard. All Windows programs except console programs must interact with the Windows API regardless of the language.

What is code page UTF 8?

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 web, and is the default for *nix-based platforms.

What is MultibyteToWideChar?

The MultibyteToWideChar function maps the character string (from single or multibyte character sets) specified by lpMultiByteStr into a UTF-16 Unicode wide character string, using the dwFlags options and the code page specified by CodePage , and stores the result in lpWideCharStr .

What is Pcwstr?

An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated.


2 Answers

No. Use MultiByteToWideChar to convert UTF-8 to UTF-16 and then call the wide character APIs such as CreateDirectoryW or CreateFileW.

like image 96
casablanca Avatar answered Oct 07 '22 06:10

casablanca


The accepted answer is no longer correct (as of Windows Version 1903 (May 2019 Update)).

An application can now set the active code page of the process to UTF-8. This allows ...A functions (and CP_ACP) to work with UTF-8. A manifest to do that looks like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <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> 

Source and additional information: Use the Windows UTF-8 code page

like image 39
dialer Avatar answered Oct 07 '22 06:10

dialer