'How to read a non-ASCII characters in WinInet

How to use InternetReadFile to read a from pastebin with a non ASCII characters

For example this paste has russian characters which are a non ASCII characters, how to output the string to console

std::string request(const char * URL)
{
    char buffer[MAX];
    DWORD read;
    LPWSTR lpszText = new WCHAR[MAX];

    HINTERNET hSession = InternetOpen(NULL,
                                      INTERNET_OPEN_TYPE_PRECONFIG,
                                      NULL,NULL,0);
    HINTERNET conn = InternetOpenUrl(hSession,
                                     URL,
                                     NULL,
                                     0,
                                     INTERNET_FLAG_RELOAD,
                                     0);
    InternetReadFile(conn, buffer, sizeof(buffer),&read);

    ::MultiByteToWideChar(CP_UTF8,
                          MB_ERR_INVALID_CHARS,
                          buffer,read,
                          lpszText,
                          sizeof(lpszText));

    std::wcout << lpszText << std::endl;

    InternetCloseHandle(conn);
    InternetCloseHandle(hSession);
    return std::string(buffer, read);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source