0

I want to use UNICODE in visual C++ 2015 but it doesn't work so I tried a simple tutorial. Here is the code:

#undef  UNICODE
#define UNICODE
#undef  STRICT
#define STRICT
#include <windows.h>

int main()
{
    DWORD const infoboxOptions  = MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND;

    char const* const       narrowText  = "It's a 日本国 кошка, LOL!";
    wchar_t const* const    wideText    = L"It's a 日本国 кошка, LOL!";

    MessageBox( 0, wideText, L"Unicode (wide) text:", infoboxOptions );
    MessageBoxA( 0, narrowText, "ANSI (narrow) text:", infoboxOptions );
}

It doesn't work either. Both message boxes show "????" instead of special characters.

I even tried putting wWinMainCRTStartup for the entry point, defining _UNICODE in addition to UNICODE, but nothing works.

Maybe it is because that example is not adapted to visual c++ 2015?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
didonc
  • 11
  • 1
  • Unable to reproduce. – user4581301 Jul 14 '16 at 21:07
  • 1
    What about source-file encoding? Try `UTF-8` – PcAF Jul 14 '16 at 21:07
  • 3
    When I attempt to reproduce this in Visual Studio 2013, it warns me that it can't save the file and offers to save it as Unicode; I select UTF-8 with signature instead. Then it gives me warnings on the line creating the `char` constant but not the `wchar_t` constant. The `MessageBox` works for me but the `MessageBoxA` doesn't, exactly as I'd expect. – Mark Ransom Jul 14 '16 at 21:08
  • PcAF may have it nailed. If I deliberately bork the encoding I *can* reproduce. – user4581301 Jul 14 '16 at 21:10
  • Thx. How do I set source-file encoding to UTF-8? – didonc Jul 14 '16 at 21:14
  • I have "Auto-detect UTF-8 encoding without signature" already ticked off – didonc Jul 14 '16 at 21:16
  • The wide-char version of `MessageBoxA` is `MessageBoxW`, not `MessageBox`. `MessageBox` is a macro. – Havenard Jul 14 '16 at 21:49
  • @Havenard: When `UNICODE` is #defined before `windows.h` is #included, `MessageBox` is the same as `MessageBoxW` – librik Jul 15 '16 at 02:49

1 Answers1

0

The solution is in this another post: on the cpp file, Save As... and select "Save With Encoding..." and selecting "UTF-8 without signature"

Community
  • 1
  • 1
didonc
  • 11
  • 1
  • 3
    On Windows you might find it better to save UTF-8 *with* signature. You're more likely to get consistent operation within Visual Studio and other Windows utilities. Without signature is better when you're passing the file to non-Windows systems. – Mark Ransom Jul 14 '16 at 22:13