c++windows

CHAR array to std::string


When the title is "Pesquisa de domínio GoDaddy - Brave"

    CHAR title[256];
    int r = GetWindowTextA(hwnd, title, 256); // r: 35
    std::string windowTitle(title);

    std::string str = "Pesquisa de domínio GoDaddy - Brave";
    auto s1 = QByteArray::fromStdString(windowTitle).toBase64();
    auto s2 = QByteArray::fromStdString(str).toBase64();

The conversion of the char array to std::string doesn't work properly there's an error on the: í.

What's the proper way to convert a CHAR array containing these kind of 'special' characters to a std::string without relying on using GetWindowTextW and/or a wstring?

Values seen on debugger (VS22):

enter image description here


Solution

  • If you create a manifest and set the encoding to utf8:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <application>
        <windowsSettings>
          <activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
        </windowsSettings>
      </application>
    </assembly>
    

    The result of both strings will match.