size : 2675
uploaded_on : Wed Oct 14 00:00:00 1998
modified_on : Wed Dec 8 14:03:18 1999
title : Data in executable
org_filename : DataInExe.txt
author : Peter Below
authoremail :
description : How to include and retrieve data in/from exe
keywords :
tested : not tested yet
submitted_by : Mike Orriss
submitted_by_email : mjo@3kcc.co.uk
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : plain
file-type : text/plain
category : delphi-misc
__END_OF_HEADER__
You can include any kind of data as a RCDATA or user type resource. This is very simple. The following is from a message on a related item and should show you the general technique.
Type
TStrItem = String[39]; { 39 characters + length byte -> 40 bytes }
TDataArray= Array [0..7, 0..24] of TStrItem;
Const
Data : TDataArray = (
('..', ...., '..' ), { 25 strings per row }
... { 8 of these rows }
('..', ...., '..' )); { 25 strings per row }
The data will end up in your data segment and consume 8K of it. If that is too much for you, put the actual data into a RCDATA resource. For that you proceed like this. Write a small windowless program that declares the typed constant as above and does nothing more than write it to a disk file:
Program MakeData;
Type
TStrItem = String[39]; { 39 characters 0 length byte -> 40 bytes }
TDataArray= Array [0..7, 0..24] of TStrItem;
Const
Data : TDataArray = (
('..', ...., '..' ), { 25 strings per row }
... { 8 of these rows }
('..', ...., '..' )); { 25 strings per row }
Var
F: File of TDataArray;
Begin
Assign( F, 'data.dat' );
Rewrite(F);
Write(F, Data);
Close(F);
End.
Now prepare a resource file, lets call it DATA.RC. It contains only the following line:
DATAARRAY RCDATA "data.dat"
Save it, open a DOS box, change to the directory you saved data.rc to (same as data.dat is in!), run the following command:
brcc data.rc (brcc32 for Delphi 2.0)
That gives you a data.res file you can now include in your Delphi project. At runtime you can now generate a pointer to that resource data and access it as needed.
{ in a Unit interface section }
Type
TStrItem = String[39]; { 39 characters + length byte -> 40 bytes }
TDataArray= Array [0..7, 0..24] of TStrItem;
PDataArray= ^TDataArray;
Const
pData: PDataArray = Nil; { use a Var in Delphi 2.0}
Implementation
{$R DATA.RES}
Procedure LoadDataResource;
Var
dHandle: THandle;
Begin
{ pData := Nil; if pData is a Var }
dHandle := FindResource( hInstance, 'DATAARRAY' , RT_RCDATA );
If dHandle <> 0 Then Begin
dhandle := LoadResource( hInstance, dHandle );
If dHandle <> 0 Then
pData := LockResource( dHandle );
End;
If pData = Nil Then
{ failed, issue appropriate error message, use WinProcs.MessageBox,
not VCL stuff, since the code here will execute as part of the
program startup sequence and VCL may not have initialized yet! }
End;
Initialization
LoadDataResource;
End.
Now you can refer to items in the array with pData^[i,j]-syntax.
- Peter Below