0

I am developing a Win32 desktop application with C++ where the target-language is Swedish. As a result of that I rely on Unicode to properly format and display any given string. However, I encountered a very peculiar issue where strings consisting of non-english characters (such as å, ä, ö) work in one part of the code, but breaks in another part of the code.

More specifically, the code works in one file, but not the other. If I move the code-block to the other file, then the text is properly displayed again. See the following example:

CreateWindowW(L"Button", L"Lägg till kund", WS_TABSTOP | WS_VISIBLE | WS_CHILD, 
              0, 0, 80, 25, windowHandle, (HMENU)0, NULL, NULL);

-Which is called from the Application.cpp file.

AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_ArkivMenu, L"&Arkiv");
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_VisaMenu, L"&Visa");
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_ArkivMenu, L"&Hjälp");
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_ArkivMenu, L"&Sök");

-Which is called from the Window.cpp file.

All of this results in the following:

Calling code from different headers

Note that the code called from Window.cpp generates text with invalid characters, which indicates that the symbol(s) couldn't be found. This is very strange since the problem ceases to exist if I move the code from Window.cpp to Application.cpp.

Thus, the only logical conclusion is that the character encoding must differ between these two files, but why?

2
  • Maybe you can find an answer here: stackoverflow.com/questions/22893226/… Commented Jul 1, 2021 at 12:22
  • Difficult to say why it's different. It's caused by the way you created and/or you saved the files. Commented Jul 1, 2021 at 12:23

1 Answer 1

2

The problem is indeed, as the title states, "Character encoding not consistent".

And to be precise, that's the character encoding of Window.cpp compared to the character encoding of Application.cpp. I suspect the environment is Visual Studio, which can handle files in multiple encodings. But for source code that contains Unicode, you probably want to use UTF-8 with "signature" (BOM).

This is available in Visual Studio 2017+ via "Save As", and in that save dialog choose "Save With Encoding" instead of plain "Save".

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It turns out that one of the files, more specifically Application.h used a western european DOS signature. I have no clue as to why this occured in the first place but the problem is now fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.