Friday, November 8, 2019
How to Add Resource Files in Delphi Executables
How to Add Resource Files in Delphi Executables Games and other types of applications which use multimedia files like sounds and animations must either distribute the extra multimedia files along with the application or embed the files within the executable. Rather than distribute separate files for your applications use, you can add the raw data to your application as a resource. You can then retrieve the data from your application when it is needed. This technique is generally more desirable because it can keep others from manipulating those add-in files. This article will show you how to embed (and use) sound files, video clips, animations and more generally any kind of binary files in a Delphi executable. For the most general purpose, youll see how to put an MP3 file inside a Delphi exe. Resource Files (.RES) In the Resource Files Made Easy article you were presented with several examples of the use of bitmaps, icons, and cursors from resources. As stated in that article we can use the Image Editor to create and edit resources that consist of such types of files. Now, when we are interested in storing various types of (binary) files inside a Delphi executable well have to deal with resource script files (.rc), the Borland Resource Compiler tool and other. Including several binary files in your executable consists of 5 steps: Create and/or collect all the files you wish to put in an exe.Create a resource script file (.rc) that describes those resources used by your application,Compile the resource script file (.rc) file to create a resource file (.res),Link the compiled resource file into the applicationââ¬â¢s executable file,Use individual resource element. The first step should be simple, simply decide what types of files you would like to store in your executable. For example, we will store two .wav songs, one .ani animations and one .mp3 song. Before we move on, here are a few important statements concerning limitations when working with resources: Loading and unloading resources is not a time-consuming operation. Resources are part of the applications executable file and are loaded at the same time the application runs.All the (free) memory can be used when loading/unloading resources. In other words, there are no limits on the number of resources loaded at the same time.Of course, resource files do double the size of an executable. If you want smaller executables, consider placing resources and parts of your project in a dynamic link library (DLL) or its more specialized variation. Lets now see how to create a file that describes resources. Creating a Resource Script File (.RC) A resource script file is a just a simple text file with the extension .rc that lists resources. The script file is in this format: ResName1 ResTYPE1 ResFileName1ResName2 ResTYPE2 ResFileName2...ResNameX ResTYPEX ResFileNameX... RexName specifies either a unique name or an integer value (ID) that identifies the resource. ResType describes the type of resource and the ResFileName is the full path and file name to the individual resource file. To create a new resource script file, simply do the following: Create a new text file in your projects directory.Rename it to AboutDelphi.rc. In the AboutDelphi.rc file, have the following lines: Clock WAVE c:\mysounds\projects\clock.wavMailBeep WAVE c:\windows\media\newmail.wavCool AVI cool.aviIntro RCDATA introsong.mp3 The script file simply defines resources. Following the given format the AboutDelphi.rc script lists two .wav files, one .avi animation, and one .mp3 song. All statements in a .rc file associate an identifying name, type and file name for a given resource. There are about a dozen predefined resource types. These include icons, bitmaps, cursors, animations, songs, etc. The RCDATA defines generic data resources. RCDATA let you include a raw data resource for an application. Raw data resources permit the inclusion of binary data directly in the executable file. For example, the RCDATA statement above names the applicationââ¬â¢s binary resource Intro and specifies the file introsong.mp3, which contains the song for that MP3 file. Note: make sure you have all the resources you list in your .rc file available. If the files are inside your projects directory you dont have to include the full file name. In my .rc file .wav songs are located *somewhere* on the disk and both the animation and MP3 song are located in the projects directory. Creating a Resource File (.RES) To use the resources defined in the resource script file, we must compile it to a .res file with the Borlands Resource Compiler. The resource compiler creates a new file based on the contents of the resource script file. This file usually has an .res extension. The Delphi linker will later reformat the .res file into a resource object file and then link it to the executable file of an application. The Borlands Resource Compiler command line tool is located in the Delphi Bin directory. The name is BRCC32.exe. Simply go to the command prompt and type brcc32 then press Enter. Since the Delphi\Bin directory is in your Path the Brcc32 compiler is invoked and displays the usage help (since it was called with no parameters). To compile the AboutDelphi.rc file to a .res file execute this command at the command prompt (in the projects directory): BRCC32 AboutDelphi.RC By default, when compiling resources, BRCC32 names the compiled resource (.RES) file with the base name of the .RC file and places it in the same directory as the .RC file. You can name the resource file anything you want, as long as it has the extension .RES and the filename without the extension is not the same as any unit or project filename. This is important because, by default, each Delphi project that compiles into an application has a resource file with the same name as the project file, but with the extension .RES. Its best to save the file to the same directory as your project file. Including (Linking/Embedding) Resources to Executables After the .RES file is linked to the executable file, the application can load its resources at run time as needed. To actually use the resource, youll have to make a few Windows API calls. In order to follow the article, youll need a new Delphi project with a blank form (the default new project). Of course add the {$R AboutDelphi.RES} directive to the main forms unit. Its finally time to see how to use resources in a Delphi application. As mentioned above, in order to use resources stored inside an exe file we have to deal with API. However, several methods can be found in the Delphi help files that are resource enabled. For example, take a look at the LoadFromResourceName method of a TBitmap object. This method extracts the specified bitmap resource and assigns it TBitmap object. This is *exactly* what LoadBitmap API call does. As always Delphi has improved an API function call to suit your needs better. Now, add the TMediaPlayer component to a form (name: MediaPlayer1) and add a TButton (Button2). Let the OnClick event look like: One minor *problem* is that the application creates an MP3 song on a user machine. You could add a code that deletes that file before the application is terminated. Extracting *. Of course, every other type of a binary file can be stored as a RCDATA type. The TRsourceStream is designed specially to help us extract such file from an executable. The possibilities are endless: HTML in an exe, EXE in exe, empty database in an exe, and so and so forth.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.