【问题标题】:Delphi Convert array of Byte to TBytes - UDP redirect?Delphi将字节数组转换为TBytes - UDP重定向?
【发布时间】:2013-11-07 14:38:50
【问题描述】:
procedure TForm1.UDPUDPRead(AThread: TIdUDPListenerThread; 
                              AData: array of Byte; ABinding: TIdSocketHandle);
var
  buffer : TBytes;
begin
  SetLength(buffer, Length(AData));
  buffer := @AData[0];
 end;

此代码导致访问冲突。

在 Delphi XE3 中从 字节数组 转换为 TBytes 的正确方法是什么?

【问题讨论】:

  • AData 参数不应该是TIdBytes 类型吗? (TUDPReadEvent)
  • @TLama 我认为这就是 emba 搞砸了

标签: delphi casting udp type-conversion delphi-xe3


【解决方案1】:

您需要复制缓冲区。

Count := Length(AData);
SetLength(buffer, Count);
if Count <> 0 then
  Move(AData[0], buffer[0], Length(AData));

我感觉 Indy 的这一部分被 Embarcadero 搞砸了。请注意按值传递数组的可疑传递。如果我记得,来自 repo 的 Indy 版本更好。

【讨论】:

  • Embarcadero screwed up in XE3 只是因为他们将 AData 参数从动态数组更改为开放数组,但这并没有改变 wittrup 将数据从一个数组复制到另一个数组的代码是一开始就是完全错误的。所以是的,使用Move() 来解决这个问题。
  • @Arioch'The 它确实按值传递。更重要的是制作了一份副本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
相关资源
最近更新 更多