简体   繁体   English

Delphi 10:SizeOf(string)问题-Windows vs Android

[英]Delphi 10: Problems with SizeOf(string) - Windows vs Android

I want to send TMemoStream from Android to Windows using Indy IdTCPClient and IdTCPServer. 我想使用Indy IdTCPClient和IdTCPServer将TMemoStream从Android发送到Windows。

The problem is this: 问题是这样的:

SizeOf(string) in Android is 4 bytes
SizeOf(string) in Windows 10 is 8 bytes

In android I used this code: 在android中,我使用了以下代码:

type TMyRecord = record
  x1: string;
  x2: string;
end;

var
  workRecord: TMyRecord;
  rInfo: TMemoryStream;
begin
  workRecord.x1:= 'Hello';
  workRecord.x2:= 'How are you';
  rInfo:= TMemoryStream.Create;

  try
    rInfo.Write(workRecord, SizeOf(workRecord));  //Size of workRecord is 8 bytes
    AndroidTCPClient.IOHandler.Write(workRecord, 0, False);
  finally
    rInfo.Free;
  end;
end;

In Windows 10 I used this code: 在Windows 10中,我使用了以下代码:

type TMyRecord = record
  x1: string;
  x2: string;
end;

type TMyRecord = record
  x1: string;
  x2: string;
end;

var
  workRecord: TMyRecord;
  rInfo: TMemoryStream;
begin
  rInfo:= TMemoryStream.Create;

  try
    AContext.Connection.IOHandler.ReadStream(rInfo, SizeOf(workRecord), False);
    rInfo.Position:= 0;
    rInfo.Read(workRecord, SizeOf(workRecord)); //Size of workRecord is 16 bytes
  finally
    rInfo.Free;
  end;
end;

Anyone advise me how to set up the transmission string from Android to Windows? 有人建议我如何设置从Android到Windows的传输字符串吗?

sizeof(string) is the size of a pointer. sizeof(string)是指针的大小。 Your Windows program is clearly compiled for 64 bit and so pointers are 8 bytes wide. Windows程序显然是为64位编译的,因此指针的宽度为8个字节。 Your Android program targets 32 bit and pointers are 4 bytes wide. 您的Android程序的目标是32位,指针的宽度为4个字节。

The bigger problem is that you cannot expect to send pointers from one process to another. 更大的问题是,您不能期望将指针从一个进程发送到另一个进程。 The pointers refer to memory in the sending program. 指针指向发送程序中的内存。 They have no meaning in the recipient. 它们在接收者中没有意义。 You should probably serialize your data, for instance to JSON, and then send that. 您可能应该将数据序列化(例如,序列化为JSON),然后将其发送。 Then the recipient can deserialize upon receipt. 然后,接收者可以在收到收据后反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM