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?
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.
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.
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 .
An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated.
No. Use MultiByteToWideChar
to convert UTF-8 to UTF-16 and then call the wide character APIs such as CreateDirectoryW
or CreateFileW
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With