File compiled into a delphi exe.

Hi,
I'm using delphi 3.02

Is it possible to include a file (text or otherwise) into the compiled exe of a delphi program. Ie the file is then burned into the exe and doesn't have to be distributed as a seperate file with the application. I am looking at a static file here, so there is no need to update it - just access it. If so, say for example a text file, then how does one access it. (I have had a quick look at resource files, but they seem way too complicated for my liking)

thanks
clyde99Asked:
Who is Participating?
 
intheConnect With a Mentor Commented:
for files inside a resource (especially exe's) you must extract them to disk bfore they can b used
make a .rc file that looks like:

 IDR_RE_TEXT_DEMO  RCDATA C:\TEST.EXE
and save as myres.rc.
compile with brcc32 to get myres.res

then in unit do:

unit Main;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  Menus, StdCtrls, ComCtrls;
 
type
  TMainForm = class(TForm)
    MainRichEdit: TRichEdit;
    procedure FormCreate(Sender: TObject);
  private
    procedure LoadTextFromResource;
  public
    { Public declarations }
  end;
 
var
  MainForm: TMainForm;
 
implementation
 
{$R *.DFM}
{$R MYRES.RES}
 
 
procedure TMainForm.LoadTextFromResource;
var
  ResID  : Word;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;  
begin
  ResID := IDR_RE_TEXT_DEMO;
  //Locates the resource and returns a handle to it. If the function   //fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource; otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          MainForm.MainRichEdit.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;
 
procedure TMainForm.FormCreate(Sender: TObject);
begin    
  LoadTextFromResource;
end;
 
end.

THATS A ROUGH EXAMPLE ;-)
see thius project for examples of other stuff you can put in resoursce :
http://www.bhnet.com.br/~simonet/archive/resonlydll.zip

Regards Barry
0
 
XANCommented:
it's good idea to use resource file. other way for text file - include text in project as resourcestring on string constant (if it is not too large).
However I advise use resource file - it will optimal for any reason.
0
 
intheCommented:
hi,
ignore my last post its a bit erratic :-)
resource files are complicated at firest but once you atsrt using them you get use to them and can be quite handy.

i done better tested example and tried to comment the procedure for making them .:

first make a demo project directory  for this then
go to hard drive and find a text file and re-name it test.txt or (make one)

open notepad and type the following line:

TEXT_DEMO  RCDATA C:\test.txt

save this as myres.rc  and put in in delphis bin dir.

TEXT_DEMO is the idendifier ,rcdata is just a datatype and c:\test.txt is the path to your txt file.
 
go to command prompt delphi\bin directory compile myres.rc with brcc32.exe.
if you havent used it before you just at the command prompt type:
brcc32 myres.rc
and you will get a compiled one called myres.res ,put myres.res in your projects directory so it can find it.

now you have your static text file inside a resource file.


in delphi:
create a new project and save it in your project dir that you made where the myres file resides.

drop a richedit on the form
and got to code and add in the private section the following line:
 procedure LoadTextFromResource;
then add in
{myres.rc} in the line below {$R *.DFM}

then add the following procedure:

procedure TForm1.LoadTextFromResource;
var
  ResID  : String;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;
begin
  ResID := 'TEXT_DEMO';
  //Locates the resource and returns a handle to it.
  // If the function fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          Form1.RichEdit1.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;


now drop a button on the form and  make the buttons click event like:

procedure TForm1.Button1Click(Sender: TObject);
begin
LoadTextFromResource;
end;


now compile and run and when the button is clicked the richedit will be filled with your test file...

your complete unit will be like:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure LoadTextFromResource;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{$R Myres.RES}

procedure TForm1.LoadTextFromResource;
var
  ResID  : String;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;
begin
  ResID := 'TEXT_DEMO';
  //Locates the resource and returns a handle to it.
  // If the function fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          Form1.RichEdit1.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
LoadTextFromResource;
end;

end.


hope that a help and not too long a posting ;-)

Regards Barry




0
 
clyde99Author Commented:
Thanks Barry. Finally an explanation of resource files that I understand :-)
0
 
intheCommented:
no probs ;-)
0
All Courses

From novice to tech pro — start learning today.