Home > Back-end >  Which encoding works best for Windows API calls?
Which encoding works best for Windows API calls?

Time:08-06

I use a function from the Windows API called GetFileAttributesW to retrieve attributes from a file. The function signature is defined as:

DWORD GetFileAttributesW([in] LPCWSTR lpFileName);

LPCWSTR is defined as const wchar_t*.

I want to call the function:

fs::path inputPath = ...
GetFileAttributesW(inputPath.whichMethodHere()?);

The given input path is of type std::filesystem::path that has several convenience converters like:

std::filesystem::path::string()
std::filesystem::path::wstring()
std::filesystem::path::u8string()
std::filesystem::path::u16string()
std::filesystem::path::u32string()

The two functions wstring() and u16string() stand out here for me. Both are defined with a character type of wchar_t and char16_t respectively.

Question 1:

What is the main difference between wstring() and u16string(). Does wstring() return something different than u16string in real life scenarios?

Question 2:

Which encoding does the Windows API generally expect?

CodePudding user response:

The difference between std::wstring and std::u16string is that std::wstring uses wchar_t and std::u16string uses char16_t, which you managed to figure out by yourself.

wchar_t is not required to use UTF-16 encoding. Linux uses UTF-32 for wchar_t for example, while char16_t is required to use UTF-16 everywhere.

As the parameter is LPCWSTR the expected type of that parameter is const wchar_t*, which you also managed to figure out by yourself.

Windows functions which ends in A expects char pointers, and windows functions which ends in W expects wchar_t pointers. There are also macros which does not end in either, and these macros expand to either the A version or the W version depending on whether unicode is enabled during compilation.

Note: There is also std::filesystem::path::native() if you want the string as the native string type.

My recommendation would be GetFileAttributesW(inputPath.c_str()); as c_str returns a 'native' pointer.

  • Related