Extracting data from a program's resources (Views: 102)
Sometimes it's useful to embed some data (or a file) inside our program's resources. This article deals with how to get the data out again
This article assumes that the data you embedded was in the RCDATA format. This is a user-defined data format so we also assume that you know how to decode the format.
Reading it back from the resource is very simple due to the TResourceStream class that ships with Delphi. You create a TResourceStream in one of two ways
// 1: Do this if the resource is named
RS := TResourceStream.Create(
HInstance, // your app or DLL instance handle
ResourceName, // string containing resource name
RT_RCDATA); // identifies RCDATA resource type
// 2: Do this if it is identified by an ordinal
// value (per my article on writing the data)
RS := TResourceStream.CreateFromID(
HInstance, // as above
ResourceID, // Word identifier
RT_RCDATA); // as above
You now just use the stream to read the data as if it was coming from a file. You can't write to a TResourceStream as data embedded in a executable file is read only.
To copy your embedded file into another stream (say AS of type TStream) you can use this code:
As an example, say we have a program that includes an resource file that contains an RCDATA resource which has a copy of a rich text file inside it. The program displays the embedded rich text in a rich edit component. This is the code we need:
// Create resource stream (resource id is 100)
RS := TResourceStream.CreateFromID(HInstance,
// Load the rich edit component
// Free the stream
You can download a worked example that demonstrates what has been described here -- it uses the above code. The .zip file contains a pair of projects. The first a program that embeds a supplied rich text file in a resource file. The second program includes the resource file and displays the rich text in a rich edit component.
<< Back to main page