Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top