microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.91k stars 1.64k forks source link

Locale bug #1715

Closed CRC32EX closed 1 year ago

CRC32EX commented 1 year ago

Steps to reproduce this issue

  1. Write code

vcpkg.json

{
  "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
  "name": "websocket-test",
  "version": "1.0.0",
  "dependencies": [
    {
      "name": "cpprestsdk",
      "features": [ "websockets" ]
    }
  ]
}

Source.cpp

#include <cpprest/ws_client.h>

int main() {
    std::locale::global(std::locale("Japanese"));

    web::websockets::client::websocket_callback_client client;
    client.connect(U("wss://example.com:7000/")).then([&client]() {
        std::wcout << L"connected" << std::endl;
    });

    while (true) { Sleep(1000); }
    return 0;
}
  1. Execute
  2. connected not appear.

Possible solution

Remove this line works successfully. I don't know why.

std::locale::global(std::locale("Japanese"));

Environment

barcharcraz commented 1 year ago

According to https://docs.microsoft.com/en-us/cpp/c-runtime-library/country-region-strings?view=msvc-170 Japanese is not actually a valid "language string", which surprised me.

CRC32EX commented 1 year ago

I'm sorry, It's not cpprestsdk side problem. I think.

Remove this line

std::locale::global(std::locale("Japanese"));

and, use these APIs to emit Japanese strings to console. Solved.

MultiByteToWideChar
WideCharToMultiByte

ref: https://nekko1119.hatenablog.com/entry/2017/01/02/054629