How to Embed Base64 Resources in an EXE (C++)
An Elegant Self Installing Self Extraction
The concept here is simple. We are developing with Visual Studio Express and need to distribute a program that has resources (ico, ,jpeg, jpg, png, dll).We do not want to build an installer since that requires a lot of financial overhead. We do not wish to create Click Once and signed code because that requires an expensive yearly certificate renewal.
Instead we may simply embed resources in the program as base64 strings, and later write these resources out to a files if we discover they are missing from the disk. We may also verify the resource integrity if so inclined.
You will need base64.cpp
If you are looking for an easy way to base64 encode files try the Base64 DataURI Creator.
This example relies on managed System::String for System::IO::File::Exists and System::Windows::Forms::MessageBox which may require the .NET framework. Also utilizes Application::StartupPath to notify user where files will be created. There are other ways to achieve the same results in these cases, however since I was using .NET this seemed elegant enough. Here is a simple C based file_exits function.
This is what my resources.h looks like:
That's all there is too it! Sample Usage:
An easy way to base64 encode files is Base64 DataURI Creator. Please note that this example does not embed the DataURI header in front of the base64 data. In some cases you may find ways to use resources without saving them to disk.
It is recommended that you place extracted resources in a directory so that the user may easily clean up extraneous files.
If you would like to see this concept in action try the windows version of ColorPick.
Comments