2
votes
procedure TForm1.UDPUDPRead(AThread: TIdUDPListenerThread; 
                              AData: array of Byte; ABinding: TIdSocketHandle);
var
  buffer : TBytes;
begin
  SetLength(buffer, Length(AData));
  buffer := @AData[0];
 end;

This code results in an access violation.

What would be the proper way to convert from array of byte to TBytes in Delphi XE3?

1
Shouldn't that AData parameter be of TIdBytes type ? (TUDPReadEvent) - TLama
@TLama I think that's what emba screwed up - David Heffernan

1 Answers

5
votes

You need to copy the buffer.

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

I have a feeling that this part of Indy was screwed up by Embarcadero. Note the dubious passing of array by value. If I recall, the version on Indy from the repo is better.