Recent

Author Topic: tmemorystream.setpointer  (Read 3180 times)

fcu

  • Jr. Member
  • **
  • Posts: 89
tmemorystream.setpointer
« on: February 17, 2019, 02:16:11 pm »
Hi
i have an empty memorystream variable , i want assign to it an array of bytes
in delphi there is tmemorystream.setpointer(pointer,size)

is there something similar ? 

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: tmemorystream.setpointer
« Reply #1 on: February 17, 2019, 02:24:56 pm »
Use a TByteStream?
Code: Pascal  [Select][+][-]
  1. AStream := TBytesStream.Create(MYArrayOfBytes);

or simply write the array to the stream:
Code: Pascal  [Select][+][-]
  1. MyStream.WriteBuffer(MYArray, SizeOf(MyArray));
« Last Edit: February 17, 2019, 02:28:18 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: tmemorystream.setpointer
« Reply #2 on: February 17, 2019, 05:17:28 pm »
thanks , it works

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: tmemorystream.setpointer
« Reply #3 on: February 17, 2019, 06:49:48 pm »
in delphi there is tmemorystream.setpointer(pointer,size)
Same thing, there is a protected method in FPC:
Code: Pascal  [Select][+][-]
  1. procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; ASize: PtrInt);
In Delphi method also protected:
Code: Pascal  [Select][+][-]
  1. procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; const Size: NativeInt);
   

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: tmemorystream.setpointer
« Reply #4 on: February 17, 2019, 07:46:49 pm »
I am rather sure that using SetPointer by type-cast hacking from outside the TMemorystream is calling for trouble because it confuses the memory allocation done by TMemorystream.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: tmemorystream.setpointer
« Reply #5 on: February 17, 2019, 08:03:32 pm »
I am rather sure that using SetPointer by type-cast hacking from outside the TMemorystream is calling for trouble because it confuses the memory allocation done by TMemorystream.
No hacking. Example:
Code: Pascal  [Select][+][-]
  1. type
  2.   TReadOnlyMemStreamOnBuffer = class(TCustomMemoryStream)
  3.   public
  4.     constructor Create(const OnBuffer; BufSize: PtrInt);
  5.   end;
  6.  
  7. constructor TReadOnlyMemStreamOnBuffer.Create(const OnBuffer; BufSize: PtrInt);
  8. begin
  9.   inherited Create;
  10.   SetPointer(@OnBuffer, BufSize);
  11. end;

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: tmemorystream.setpointer
« Reply #6 on: February 17, 2019, 09:48:05 pm »
in delphi there is tmemorystream.setpointer(pointer,size)
Same thing, there is a protected method in FPC:
Code: Pascal  [Select][+][-]
  1. procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; ASize: PtrInt);
In Delphi method also protected:
Code: Pascal  [Select][+][-]
  1. procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; const Size: NativeInt);
   

you are right its protected in both , i didn't noticed that
anyway stream.write or writebuffer is what i was looking for
and stream.Seek(0,soFromBeginning) is also needed after finish writing to the buffer

but now i have a strange problem , i wrote a 2 version of funtion that load png image (from file or memory ) and return pSDL_Texture
Code: Pascal  [Select][+][-]
  1.  function LoadFromMemory(const ptr : pointer; const size : longint):pSDL_Texture;
with this declaration i got this exception :
Code: Pascal  [Select][+][-]
  1. An unhandled exception occurred at $0040F332:
  2. FPImageException: Wrong image format
  3.  

but with this declaration it works fine
Code: Pascal  [Select][+][-]
  1. function LoadFromMemory(const ptr : array of byte; const size : longint):pSDL_Texture;

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: tmemorystream.setpointer
« Reply #7 on: February 17, 2019, 09:54:21 pm »
oh my bad , i forgot dereferencing the pointer in the stream.writebuffer(ptr,size);
now every things works ok

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: tmemorystream.setpointer
« Reply #8 on: February 17, 2019, 10:07:22 pm »
oh my bad , i forgot dereferencing the pointer in the stream.writebuffer(ptr,size);
now every things works ok

Ha! Nice to know I'm not the only one who forgets the hat (^) :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018