title: UTF-8 text in MFC application that uses Multibyte character set
body: <p>I am working on an application which receives text encoded in UTF-8 and needs to display it on some MFC control. The application is build using MultiByte character set (MBCS) and let's assume this cannot change.  </p>
  
  <p>I was hoping that if I convert the text I receive from UTF-8 to wide char string, I would be able to display it correctly using the <code>SetWindowTextW</code> method. To try this, I used a toy app which reads the input from a file and sets the texts of my controls.</p>
  
  <pre><code>std::wstring utf8_decode(const std::string &amp;str)
  {
      if (str.empty()) return std::wstring();
      int size_needed = MultiByteToWideChar(CP_UTF8, 0, &amp;str[0], (int)str.size(), NULL, 0);
      std::wstring wstrTo(size_needed, 0);
      MultiByteToWideChar(CP_UTF8, 0, &amp;str[0], (int)str.size(), &amp;wstrTo[0], size_needed);
      return wstrTo;
  }
  
  BOOL CAboutDlg::OnInitDialog()
  {
      std::vector&lt;std::string&gt; texts;
      texts.resize(6);
      std::fstream f("D:\\code\\sample-utf8.txt", std::ios::in);
      for (size_t i=0;i&lt;6;++i)
          std::getline(f, texts[i]);
  
      ::SetWindowTextW(GetDlgItem(IDC_BUTTON1)-&gt;m_hWnd, utf8_decode(texts[0]).c_str());
      ::SetWindowTextW(GetDlgItem(IDC_BUTTON2)-&gt;m_hWnd, utf8_decode(texts[1]).c_str());
      ::SetWindowTextW(GetDlgItem(IDC_BUTTON3)-&gt;m_hWnd, utf8_decode(texts[2]).c_str());
      ::SetWindowTextW(GetDlgItem(IDC_BUTTON4)-&gt;m_hWnd, utf8_decode(texts[3]).c_str());
      ::SetWindowTextW(GetDlgItem(IDC_BUTTON5)-&gt;m_hWnd, utf8_decode(texts[4]).c_str());
      ::SetWindowTextW(GetDlgItem(IDC_BUTTON6)-&gt;m_hWnd, utf8_decode(texts[5]).c_str());
      return TRUE;
  }
  </code></pre>
  
  <p>Having built the toy-app with MBCS, I am not getting what I would like. 
  <a href="https://i.stack.imgur.com/SCjaD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SCjaD.png" alt="enter image description here"></a></p>
  
  <p>As soon as I build the app using unicode, it all works fine
  <a href="https://i.stack.imgur.com/UCCSL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UCCSL.png" alt="enter image description here"></a></p>
  
  <p>Does this mean there is no hope of using unicode text for individual controls when I build with MBCS? If it is possible, can you give me any pointers? Thank you.</p>