peiman F.
peiman F.

Reputation: 1658

delphi read from TMemorystream without position change

i have a TMemory stream that is filled from a proccess, i need to read an other part of it real time.when i use this code :

for i := 0 to j do
begin
    FOutputStream.position:=i * 194
    stream4.CopyFrom(FOutputStream,   194 );
end;

it return wrong data because the writer process change the position. so i decided to use Memory property

stream4.CopyFrom( PByte(FOutputStream.Memory)[ i * 194 ] , 194) );

but i got this error

[DCC Error] Unit1.pas(640): E2010 Incompatible types: 'TStream' and 'Byte'

how can i handle this error?

Upvotes: 1

Views: 1012

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

You cannot use CopyFrom directly in this case, because that requires a stream, and you have a pointer.

You could solve this by creating a stream object that wrapped the memory owned by another memory stream. However that is needlessly complex. You merely need to call WriteBuffer.

stream4.WriteBuffer(PByte(FOutputStream.Memory)[i * 194] , 194);

I presume that you know this, but since you are operating from different threads when reading from and writing to the memory stream, you need to make sure that these actions account for any potential thread safety issues.

Upvotes: 6

Related Questions