Embedding user defined resources in Visual C++ binaries

For my current project I wanted the ability to package my entire C++ project (dependent DLLs and data files) as a single Windows executable. To do this I had to find a way to embed the data files into the executable at build time. There’s multiple ways to do this. I settled on using the Windows resource system, since the project is (currently) Windows only.

Add external files

  1. Add text files named “Sample.txt” and “Sample2.txt” to the project with some random text.

Add a new resource file and header

  1. Add a new resource file (.rc) to your project. This example will use ExternalResources.rc.

  2. Add a new header file (.h) to your project with the same name but a .h extension.

  3. Open up the header file and create one entry for each external file you want. Each ID must be unique – it’s a good idea to start around 1000 and go up from there. I use C_ for the definition, but it’s totally optionally. Name your resource however you want!

  1. Open up the resource file (Right click -> View code), and delete everything in the file. Copy the following code, and modify it to fit your needs. The #include <windows.h> statement allows you to declare a language for a resource, which I can cover in a later tutorial.

Accessing resource data

Loading resources

You’re all done, congrats!



%d bloggers like this: