stream - Delphi Write TResourceStream into TStream -


i have embedded exe file in resorce file. when use stream.savetofile('test.exe'); works fine,produced exe file works no error. when try stream.savetostream(stin); , error " stream write error " . what's wrong code ?

var list: tstringlist; stream: tresourcestream; stin, stout: tstream; memstream: tmemorystream; begin stream := tresourcestream.create(hinstance, 'phprogram' , rt_rcdata); try    stin := tstream.create;   stout := tstream.create;   stream.position := 0;   stream.savetostream(stin);   endecryptstream(stin, stout, 2913);   memstream.loadfromstream(stout);   memstream.savetofile('test.exe');   //stream.savetofile('test.exe');     stream.free; end; end; 

edited :

thanks david ... changed code , worked fine :

var    stream: tresourcestream;  memstream: tmemorystream;  begin    stream := tresourcestream.create(hinstance, 'testres' , rt_rcdata);    memstream := tmemorystream.create;     try       endecryptstream(stream, memstream, 2913);      memstream.savetofile('test.exe');        memstream.free;   stream.free; end; end; 

tstream abstract class. must not instantiate instances of tstream. must instantiate concrete class derived tstream, such tfilestream, tmemorystream, tstringstream, etc.

furthermore, use memstream without initialising it.

it looks need this:

  • create resource stream.
  • create memory stream.
  • call endecryptstream providing resource stream input , memory stream output.
  • call savetofile on memory stream save it.

or simpler:

  • create resource stream.
  • create file stream.
  • call endecryptstream providing resource stream input , file stream output.

one of common anti-patterns see on stack overflow excessive use of memory stream. appear want write file, why not cut out memory stream, , go straight file.

i don't particularly want write code here because can see small part of picture here, , code write wrong.

i suspect have not enabled compiler warnings , hints, or perhaps ignoring them. don't that. enable warnings , hints, , heed them.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -