whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1547)

Database (90)
Files (137)
Forms (107)
Graphic (115)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

14 Visitors Online


 
...add data to a compiled Exe-File?
Autor: Motzi
[ Print tip ]  

Tip Rating (20):  
     


function AttachToFile(const AFileName: string; MemoryStream: TMemoryStream): Boolean;
var
  
aStream: TFileStream;
  iSize: Integer;
begin
  
Result := False;
  if not FileExists(AFileName) then
    
Exit;
  try
    
aStream := TFileStream.Create(AFileName, fmOpenWrite or fmShareDenyWrite);
    MemoryStream.Seek(0, soFromBeginning);
    // seek to end of File
    // ans Ende der Datei Seeken
    
aStream.Seek(0, soFromEnd);
    // copy data from MemoryStream
    // Daten vom MemoryStream kopieren
    
aStream.CopyFrom(MemoryStream, 0);
    // save Stream-Size
    // die Streamgröße speichern
    
iSize := MemoryStream.Size + SizeOf(Integer);
    aStream.Write(iSize, SizeOf(iSize));
  finally
    
aStream.Free;
  end;
  Result := True;
end;

function LoadFromFile(const AFileName: string; MemoryStream: TMemoryStream): Boolean;
var
  
aStream: TFileStream;
  iSize: Integer;
begin
  
Result := False;
  if not FileExists(AFileName) then
    
Exit;

  try
    
aStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
    // seek to position where Stream-Size is saved
    // zur Position seeken wo Streamgröße gespeichert
    
aStream.Seek(-SizeOf(Integer), soFromEnd);
    aStream.Read(iSize, SizeOf(iSize));
    if iSize > aStream.Size then
    begin
      
aStream.Free;
      Exit;
    end;
    // seek to position where data is saved
    // zur Position seeken an der die Daten abgelegt sind
    
aStream.Seek(-iSize, soFromEnd);
    MemoryStream.SetSize(iSize - SizeOf(Integer));
    MemoryStream.CopyFrom(aStream, iSize - SizeOf(iSize));
    MemoryStream.Seek(0, soFromBeginning);
  finally
    
aStream.Free;
  end;
  Result := True;
end;

procedure TForm1.SaveClick(Sender: TObject);
var
  
aStream: TMemoryStream;
begin
  
aStream := TMemoryStream.Create;
  Memo1.Lines.SaveToStream(aStream);
  AttachToFile('Test.exe', aStream);
  aStream.Free;
end;

procedure TForm1.LoadClick(Sender: TObject);
var
  
aStream: TMemoryStream;
begin
  
aStream := TMemoryStream.Create;
  LoadFromFile('Test.exe', aStream);
  Memo1.Lines.LoadFromStream(aStream);
  aStream.Free;
end;

{

Note:

You can't proof whether additional data is attached or not.
To reach this, you would have to create a checksumm of the
MemoryStream and attach it.


Hinweis:

Es kann nicht überprüft werden ob zusätzliche Daten in der Datei
vorhanden sind. Um das zu erreichen müsste man eine Checksumme des MemoryStreams
erzeugen und ebenfalls anhängen.

}


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners