Solved

Storing files in a executable

Posted on 1998-07-22
1
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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