Confused about C++'s std::wstring, UTF-16, UTF-8 and displaying strings in a windows GUI

后端 未结 5 1667
长发绾君心
长发绾君心 2020-12-28 17:21

I\'m working on a english only C++ program for Windows where we were told \"always use std::wstring\", but it seems like nobody on the team really has much of an understandi

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 17:50

    One advantage to using std::wstring on Windows for GUI related strings, is that internally all Windows API calls use and operate on UTF-16. If you've ever noticed there are 2 versions of all Win32 API calls that take string arguments. For example, "MessageBoxA" and "MessageBoxW". Both definitions exist in , and in fact you can call either you want, but if is included with Unicode support enabled, then the following will happen:

    #define MessageBox MessageBoxW
    

    Then you get into TCHAR's and other Microsoft tricks to try and make it easier to deal with APIs that have both an ANSI and Unicode version. In short, you can call either, but under the hood the Windows kernel in Unicode based, so you'll be paying the cost of converting to Unicode for each string accepting Win32 API call if you don't use the wide char version.

    UTF-16 and Windows kernel use

提交回复
热议问题