Embed binary and image in EXE as resource with MinGW compiler
Binary data such as library and image can be embedded in exe file even though it is created by MinGW compiler. Normally the exe format contains binary file as resource object. The following files are required.
- Binary File
- Resource File (resource.rc)
- Resource Header File (resource.h)
In the resource header file, constants associated with data are defined.
#ifdef RESOURCE_H_ #define RESOURCE_H_ #define MY_BITMAP #define MY_BINOBJ #endif
In the resource file, files including binary data are defined for the constant.
#include "resource.h" MY_BITMAP BITMAP "image.bmp" MY_BINOBJ RCDATA "sample.dll"
When preparation is cmpleted, firstly I create resource object (resource.o) using windres. windres is already available if MinGW compiler has been installed.
windres resource.rc resource.o
In c++ code, we can handle binary resource as follows.
#include <windows.h> #include "resource.h" HRSRC hObj = FindResource(NULL,MAKEINTRESOURCE(MY_BINOBJ),RT_RCDATA); HGLOBAL hRes = LoadResource(NULL,hObj); unsigned char* lpbin = (unsigned char*)LockResource((unsigned char*)hRes); DWORD dwSize = SizeofResource(NULL,hObj); //Do Someting FreeResoure(hObj);
We can also handle binary resource as bitmap.
#include <windows.h> #include "resource.h" HBITMAP hBmp = (HBITMAP)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(MY_BITMAP), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.