Ausserdem schreibt er die Adresse des Strings und alles nachfolgende im
RAM in den Stream anstatt dem String selber. Ausserdem gibt es doch den TStringStream.
Delphi-Quellcode:
function streamtostr(S: TStream): string;
var
str: string;
lStringStream: TStringStream;
begin
lStringStream := TStringStream.Create;
Try
lStringStream.CopyFrom(S, 0);
Result := lStringStream.DataString;
Finally
lStringStream.Free;
End;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ms: tmemorystream;
s: string;
begin
ms:= tmemorystream.Create;
try
s := 'gsfdgfdghgfd';
ms.Write(s[1], length(s));
ms.Position := 0;
showmessage(streamtostr(ms));
finally
ms.Free;
end;
end;
// Alternativ:
procedure TForm1.Button2Click(Sender: TObject);
var
ms: tstream;
s: string;
begin
ms := tstringstream.Create('gsfdgfdghgfd');
try
ms.Position := 0;
showmessage(streamtostr(ms));
finally
ms.Free;
end;
end;
/EDIT: rote Box???