UpdateResource

Introduction

Over the years, I've seen various flavors of the question "How do I include a file or data with my program that I can extract at run-time and do something with it?" For example, the asker might have had a .WAV file or a .MDB (MS Access) file that s/he wanted to include in the actual executable itself. For one reason or another, they do not want to package the two files separately. One advantage this has over a separate data/configuration file is that it'll never get lost. If the user decides they want to start fresh, they can remove everything but the application itself, and when the application is started up again, it extracts an empty database, all without the user knowing the behind-the-scenes details.

So thre are really two questions on the table here: "How to include the file?" and "Once the file is included, how to extract it and 'use' it?" Let's look at a simple example.

Including the file

In this example, I'll show the necessary code to take the entire Windows calculator (calc.exe) and add it as a resource to some test .EXE. The .EXE could just as easily be the application where these code snippets reside. The first thing we must do is open calc.exe in read-only mode, and read the entire file into a buffer.

Updating the resource data

Now it's simply a matter of getting access to the resource data of the test file and updating it with the data pointed to by lpBuffer. The third parameter to UpdateResource() is the name we want to give the resource. I just chose an arbitrary name of 104.

At this point, you can actually open the test file with Visual Studio and see the addition of our data in the "Data" section. Be sure to open the file as a "resource"!

Extracting the data

So now your application has been delivered, and you need to extract the resource data.

Extracting the resource data uses functions from the same family as UpdateResource(). The required steps are to load the file containing the resource data, find the desired resource, load the resource, and then lock it.

Now you should be able to run the newly created file, calc2.exe! As I indicated at the beginning of the article, this can be done with any sort of file that you want to include along with your main application or DLL.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

as a matter of fact, if you take a look at the resources of procexp.exe (Process Explorer - www.sysinternals.com), you will see that it contains three streams in the BINRES resource.

All of them are executable files (in the sense of portable executable) that are extracted and executed (if necessary) at run time. The first one is a SYS file driver, the second and the third are 64 bit EXE files.

Yes, this is possible to have one file that contains one or more executable streams and to execute them.

In the case of process explorer, yes, since the embedded SYS driver must be physically installed on the disk before being started, since it is expected by the OS. But per se, an embedded PE file must not be copied on the disk before being executed. Once you know the existence and the content of an embedded PE file, you can address it, load it in memory and run it.

Actually, since the PE file is embedded in another file, it already exists on the disk!

As mentioned before the .ICO file doesn't store as a raw data. In fact .ICO file may contains one or more icons and it's logically divided into two segment: Main icon file header containing the types of the icons in the file and many standalone icon image.
The PE resources table contain also TWO type of resources: RT_GROUP_ICON and RT_ICON. The raw content of the resources are similar to the content .ICO file BUT DIFFERENT. Here is a small summery of the changes and a code to use UpdateResource for changing application's icon.

I think that ArnorBld managed already (He posted his question two years ago), but CG2i03 and others including myself run across the same problem when we want to change RT_ICON resource, so I thought I should post my code as a completion to your article.

No kidding! It stands to reason that unless the scope of the article is about error checking, the user of this code, and any other, is obviously responsible for employing such error checking practices.

Most articles omit error-checking code for brevity reasons. Competent developers will know that they must check for errors.

"Love people and use things, not love things and use people." - Unknown

"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

Works, but the icon never shows up as a resource (using VS2005, Axialis and Microangelo to check). I've searched high and low on the web and can not find any info on how to do this, guess not many people do this sort of thing Any help or ideas would be extremely welcome

The structures used to store icon images in .EXE and .DLL files (as resources) differ only slightly from those used in .ICO files. While it's possible to convert from one to the other, I don't have the time right now. I'll make a note for a later update.

Thanks - I have located information on MSDN on how to construct resource files (.res) with icons etc. and I think I can work it out. Examples would be awsome, but your article has helped me a lot already