Solved

add a resource file into your program

Posted on 2003-11-01
8
209 Views
Last Modified: 2010-04-05
Hi, how do you put a resource file into your program, like a .jpg file, and drop it ?
0
Comment
Question by:herd_bone
8 Comments
 
LVL 5

Expert Comment

by:DeerBear
Comment Utility
Hi,

This is quite simple and you can find lots of web pages speaking of this.
I would anyway like to point you to a class in the VCL : TResourceStream.

Have a look at it and have fun.

HTH,

Andrew
0
 
LVL 5

Expert Comment

by:DeerBear
Comment Utility
Sheesh,

I was forgetting! ^_^

If you mean to embed a compiled resource into the executable,  use the
{$R FileName.Res} directive! ^_^

Remember that DFM files are basically resources with the
name changed.

HTH,

Andrew
0
 
LVL 5

Expert Comment

by:Gwena
Comment Utility
It sounds like you want to be able to just drag a jpg onto your running exe and have the jpg added to the exe?


If that is right then I have a demo that does that... it is called jpgviewer, it is in some demos in a zip for a unit
called exemod.pas. You can download it here http://www.torry.net/vcl/vcltools/codertools/exemod.zip
The version of exemod.pas in the zip is out of date you can find the newer one at my website http://www.geocities.com/gacarpenter386/

here is the readme for this demo

===========================
This demo creates stand-alone jpg image viewers...

When you start it up it displays a tiny form with 3 buttons..

To add a jpg file to your jpg archive/viewer just click on the top button
---->[Add A New Jpg To Your Image Viewer]

OR better yet just drag and drop up to 1000 jpg's at a time onto the form....



Once you have added all your images you have a choice to make...

You can rebuild the current exe and change it into the new viewer
by clicking --> [Create An Image Viewer by Rebuilding This Exe]
(this is not the best course of action.. I just included this option
to show how to alter the current exe into the final at runtime)

Or you can click --> [Eject new Viewer App In This Dir as -> View.exe]
This will create a new stand-alone jpg viewer named View.exe in the current
directory.


Ok..now that you have the new viewer built just run it and the first image
will display full screen... the aspect ratio will remain correct for each image.
Just press the Backspace key to move backwards through your archive of images
and press the space-bar to move forward... clicking the left mouse button on the image
will also move forward through the pictures... to terminate the app just press the
escape key...

As this is just a simple demo I did not resample the images when converting to
the full screen size... adding resampling code would enhance the images quite a bit!

I also did not handle the jpg error messages...so if you add a bad jpg to the archive
up will pop an annoying error message when you try to view it :-(  this also happens if
you accidently add a non-jpg file to the archive.  It would be a simple matter to add
a test to weed out the bad images before they were added to the archive... but I was in
a rush to get this demo out :-)


Even though I set the max number of images at 1000 It is NOT such a good idea to
create a stand-alone viewer with that many pix in it!  It just gets too huge and takes
too long to compile the thing :-(   try and stay at <100 or so and you will have
better results.  I did make a huge 1000 pic viewer as a test... it ended up at 86mb!
.. it took about 3 minutes to compile on a Athlon 1600+ with 256mb ram... but it was
useable even on an old K6-2-500mhz with 64mb ram... although I would have hated to
wait for it to compile on that slow box ;-)

This simple demo could quickly be altered into many useful little programs...

..Gwen..

p.s. Use this code in any way you like... I consider it public domain... like all the rest of the strange software I write :-)
0
 

Expert Comment

by:mehdi_m
Comment Utility
if you want to add a simgle picture to your project, the most simple way is to use a TImage component, and load the picture (bmp, jpg, ...) into it. and when you want to access it, that here:

image1.picture. ....

it's so simple and usefull for small size JPEGS.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:herd_bone
Comment Utility
Sorry, i meant to say i need to add a compiled .exe into my program and install it in a directory.
0
 

Author Comment

by:herd_bone
Comment Utility
Can someone delete this question and refund the pts.?
0
 
LVL 2

Accepted Solution

by:
StTwister earned 65 total points
Comment Utility
Embedding files


--------------------------------------------------------------------------------

HowTo

Delphi offers a great way to store any tipe of file (avi, wav, EXE etc.) in the application (executable) by supporting resource files.

There are few steps to be done:

Creating a resource script (RC) file
Create a file MyRes.rc. In this file put the following line:
xx wave MyFile.wav

In case of adding more files, the line above has to be repeated.
xx represents the identifier for the embedded file (an identifier, that can be any name, must be put for each file) and wave represents the file type. As it can be seen, the line format is:

[resourcename] [filetype] [filename]

and in case the type of the file isn't known, filetype can be replaced with RCDATA.

Compiling MyRes.rc
Compile the RC file to a RES file by typing in the command prompt:

brcc32 MyRes.rc

After this will result MyRes.res - the resource file.

Including MyRes.res file in the project
The resource file must be mentioned in the *.dpr file or in the *.pas file, under implementation as it can be seen below:
implementation

{$R *.dfm}
{$R MyRes.res}

Playing the wave
For playing the wave from the embedded file, the function below can be used:
PlaySound('xx',hInstance,SND_RESOURCE or SND_SYNC);


--------------------------------------------------------------------------------

Embedding executables

As I said earlier, it is possible to store anything in the exe, even another exe! You have to follow the same steps as for the wave file (see above), the single difference being that the file type is exefile, not wave, so the line in the resource script file will be:

q exefile MyExe.exe,

where q is the identifier.
A reason for putting an exe in another is to execute it while running the main or just extracting it on the hard disk for a later action ;-). For extracting the embedded file on the hard disk it can be used a procedure like this:


procedure TForm1.ExtractRes(ResType,ResName,
      ResNewName:String);
var ExeRes:TResourceStream;
begin
    ExeRes:=TResourceStream.Create
      (Hinstance,Resname,Pchar(ResType));
    ExeRes.SavetoFile(ResNewName);
    ExeRes.Free;
end;

And here is an example for calling the procedure ExtractRes:


procedure TForm1.FormCreate(Sender: TObject);
var SysDir:Array[0..Max_Path] of Char;
begin
    GetSystemDirectory(SysDir,Max_Path);
    ExtractRes('exefile','q',SysDir+'\Virus.exe');
end;

0
 

Author Comment

by:herd_bone
Comment Utility
Thanks StTwister, this question is pretty old , i figured out how to do it but they didnt delete the question ands refund the points
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

12 Experts available now in Live!

Get 1:1 Help Now