发现在做C++服务器和UNITY3D通信,在传输的结构体上,因为两边类型不一样,传输接受全是乱码,搞了1天多,有了一点心得。

 

其他的类型对应,在网上很容易找到,主要是C++里面的TCHAR和C#的string对应问题。

 

ansi编码   TCHAR占用1个字节      Unicode编码 占用2个字节  

因为传输的字符串中有中文,所以采用Unicode编码,在C#这边要这么写。

//结构体采用顺序排列,采用Unicode字符集,1字节对齐
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
struct CMD_GR_LogonByAccounts
{
    public byte     Flags;      //标识
    //转为string 固定大小10字节
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
    public string   PassWord;   // 密码
 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
    public string   Accounts;    // 帐号
};

 

相关文章:

  • 2023-03-03
  • 2021-05-26
  • 2021-05-21
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
猜你喜欢
  • 2022-03-01
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案