Td: unicode message not send and revived

Created on 18 May 2018  路  4Comments  路  Source: tdlib/td

I compile tdlib in windows, normally without any issue compile ,
but in your example c# application cannot send message Unicode character like "爻賱丕賲"
convert to Unicode see like this "????"
what am i mistake?

Most helpful comment

It is a bug in TDLib 1.2.0 binding for C#. It will be fixed in TDLib 1.3.0, but currently you need to patch https://github.com/tdlib/td/blob/master/tdutils/td/utils/port/CxCli.h and replace at the end of the file code

inline std::string string_to_unmanaged(String^ str) {
  if (!str) {
    return std::string();
  }
  return msclr::interop::marshal_as<std::string>(str);
}

inline String^ string_from_unmanaged(const std::string &from) {
  return msclr::interop::marshal_as<String^>(from);
}

with

inline std::string string_to_unmanaged(String^ str) {
  if (!str || str->Length == 0) {
    return std::string();
  }

  Array<System::Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(str);
  cli::pin_ptr<System::Byte> pinned_ptr = &bytes[0];
  std::string result(reinterpret_cast<const char *>(&pinned_ptr[0]), bytes->Length);
  return result;
}

inline String^ string_from_unmanaged(const std::string &from) {
  if (from.empty()) {
    return String::Empty;
  }

  Array<System::Byte>^ bytes = REF_NEW Vector<System::Byte>(static_cast<ArrayIndexType>(from.size()));
  cli::pin_ptr<System::Byte> pinned_ptr = &bytes[0];
  for (size_t i = 0; i < from.size(); ++i) {
    pinned_ptr[i] = from[i];
  }
  return System::Text::Encoding::UTF8->GetString(bytes);
}

All 4 comments

It is a bug in TDLib 1.2.0 binding for C#. It will be fixed in TDLib 1.3.0, but currently you need to patch https://github.com/tdlib/td/blob/master/tdutils/td/utils/port/CxCli.h and replace at the end of the file code

inline std::string string_to_unmanaged(String^ str) {
  if (!str) {
    return std::string();
  }
  return msclr::interop::marshal_as<std::string>(str);
}

inline String^ string_from_unmanaged(const std::string &from) {
  return msclr::interop::marshal_as<String^>(from);
}

with

inline std::string string_to_unmanaged(String^ str) {
  if (!str || str->Length == 0) {
    return std::string();
  }

  Array<System::Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(str);
  cli::pin_ptr<System::Byte> pinned_ptr = &bytes[0];
  std::string result(reinterpret_cast<const char *>(&pinned_ptr[0]), bytes->Length);
  return result;
}

inline String^ string_from_unmanaged(const std::string &from) {
  if (from.empty()) {
    return String::Empty;
  }

  Array<System::Byte>^ bytes = REF_NEW Vector<System::Byte>(static_cast<ArrayIndexType>(from.size()));
  cli::pin_ptr<System::Byte> pinned_ptr = &bytes[0];
  for (size_t i = 0; i < from.size(); ++i) {
    pinned_ptr[i] = from[i];
  }
  return System::Text::Encoding::UTF8->GetString(bytes);
}

Thanks , yesterday i found this , it worked for me

Excellent implementation. I like it. Especially the second function with type conversion.

Fixed in 1.3.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hmartinh picture hmartinh  路  5Comments

mossaudi picture mossaudi  路  3Comments

isopen picture isopen  路  5Comments

L11R picture L11R  路  4Comments

FJarvan picture FJarvan  路  3Comments