Solved

Storing files in a executable

Posted on 1998-07-22
1
153 Views
Last Modified: 2010-04-04
Is there anyway I could use a component to store files in the main executable file. Perhaps using a TStream object in the component to store and access the file. And letting delphi store save the file data as it would with any other component property.
0
Comment
Question by:FRAME
1 Comment
 

Accepted Solution

by:
bome earned 100 total points
ID: 1358472
Well, there is a component that does just that. I don't know where I got it, so I just post it here directly.
It lets you assign a file in the object inspector. The file is then stored along with other component data in the RCData of the respective form.
In the program you access the file with the Data property (pointer to the first byte) and the DataSize property.

AUTHOR:            (C) Glen Why, 1996

URL:            eugene@actcom.co.il
            (please specify "TFileResource" in the subject string)

STATUS:            FREEWARE (source included): you can freely use or distribute files
            included to this archive.  Anyway, I'll appreciate your donations       
            (in any form: $$, bug-reports, soft, registered versions of your
            components, ideas, etc.)

unit FileResource;

interface
uses
  SysUtils, Classes;


type

  TFileResource = class( TComponent )
  private
    FFileName :TFileName;
    FData :Pointer;
    FDataSize :Integer;
    procedure ReadData( stream :tstream );
    procedure WriteData( stream :tstream );
    procedure SetFileName( const Value :TFileName );
  protected
    procedure DefineProperties( Filer :TFiler ); override;
  public
    destructor Destroy; override;
    property Data :Pointer read FData;
    property DataSize :Integer read FDataSize;
  published
    property FileName :TFileName read FFileName write SetFileName;
  end;


procedure register;

implementation
uses
  DsgnIntf, FileNameProp;

procedure register;
begin
 RegisterPropertyEditor( TypeInfo( TFileName ),
  TFileResource, 'FileName', TFileNameProperty );
  RegisterComponents ( 'more...', [ TFileResource ] );
end;


procedure TFileResource.DefineProperties( Filer :TFiler );
begin
  inherited DefineProperties( Filer );
  Filer.DefineBinaryProperty( 'DATA', ReadData, WriteData, FData <> nil );
end;

procedure TFileResource.ReadData( stream :tstream );
begin
  with stream do
   begin
     read( FDataSize, sizeof( FDataSize ) );
     GetMem( FData, FDataSize + 32 );
     try
       read( FData^, FDataSize );
     except
       FreeMem( FData );
       FData := nil;
       FDataSize := 0;
       raise;
     end;
   end;
end;

procedure TFileResource.WriteData( stream :tstream );
begin
  with stream do
   begin
     Write( FDataSize, Sizeof( FDataSize ) );
     write( FData^, FDataSize );
   end;
end;


procedure TFileResource.SetFileName( const Value :String );
begin
  if Value <> FFileName then
   with TFileStream.Create( Value, fmOpenRead ) do
    try
      FFileName := '';
      if FData <> Nil  then
        begin
         FreeMem( FData );
         FData := nil;
        end;
      FDataSize := Size + 32;
      GetMem( FData, FDataSize );
      try
        read( FData^, FDataSize );
      except
        FreeMem( FData );
        FDataSize := 0;
        FData := nil;
        raise;
      end;
      FFileName := Value;
    finally
      free;
    end;
end;


destructor TFileResource.Destroy;
begin
  if FData <> Nil then FreeMem( FData );
  inherited Destroy;
end;

end.

bome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now