How to Embed a File into an Executable

Embed File in a Executable

You want to do embed the file as a resource.

Right-click the project file, select Properties.

In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.

In your code you can type in Resources.TheNameYouGaveTheFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).

Do you need help with the saving of the file also?

Edit:

You could do something like this:

var resource = Properties.Resources.yourResource;

FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
for (int i = 0; i < resource.Length; i++)
fileStream.WriteByte((byte)resource[i]);
fileStream.Close();

Does it help you?

EDIT:

As I can see you are getting a stream, here is an update to make it work:

var resource = Properties.Resources.yourResource;

FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
resource.CopyTo(fileStream);
fileStream.Close();

Does it work now?

How to embed a file into an executable?

A portable way is to define a function like

typedef unsigned char Byte;

Byte const* pngFileData()
{
static Byte const data =
{
// Byte data generated by a helper program.
};
return data;
}

Then all you have to do is to write a little helper program that reads the PNG file as binary and generates the C++ curly braces initializer text. Edit: @awoodland has pointed out in comment to the question, that ImageMagick has such a little helper program…

Of course, for a Windows-specific program, instead use the ordinary Windows resource scheme.

Cheers & hth.,

How to embed a file into an executable file?

I think that for this to work with MinGW you'll need to remove the leading underscore from the names in the .c file. See Embedding binary blobs using gcc mingw for some details.

See if using the following helps:

extern char binary_data_txt_start;
extern char binary_data_txt_end;

If you need the same source to work for Linux or MinGW builds, you might need to use the preprocessor to have the right name used in the different environments.

Embed a file into an external exe with C#

So after some intense googling I found a library called Mono.Cecil which does exactly what I want. I was able to inject a file into an executable file by using the following code:

string fileName = "Interpreter.exe"; // The file which will have toInject injected in it
string outputName = "Compiled.exe"; // The output file to compile
string toInject = "program.txt"; // The file we will be injecting into fileName
string resourceName = "program.txt"; // The name of the file once it's inside fileName

var module = ModuleDefinition.ReadModule(filename);

var file = new EmbeddedResource(
resourceName,
ManifestResourceAttributes.Private,
File.ReadAllBytes(toInject));

module.Resources.Add(file);

module.Write(outputName);

How to embed an exe file into another exe file as a resource in C++?

If your #define is already in resource.h, there is no need to duplicate it in your source code. Just use #include "resource.h" in your code instead.

In any case, you should be using the pre-defined RCDATA resource type, instead of creating a custom BINARY type.

That being said, your use of ofstream and system() are both wrong. You are passing them the resource's raw binary data, but they are expecting a file path instead. You are using a file path in your .rc file to specify the file whose binary data is copied into the resource. The resource does not contain that file path, as you are clearly expecting.

Try this instead:

resource.h

...
#define IDB_EMBEDEXE 52
...

Resource.rc

#include "resource.h"
...
IDB_EMBEDEXE RCDATA "C:\\Users\\Almohandis\\source\\repos\\Project7\\sum2num.exe"
...

Source.cpp

#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include "resource.h"
using namespace std;

int main() {

HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(IDB_EMBEDEXE), RT_RCDATA);
if (!hResource)
return 1;

HGLOBAL hGlobal = LoadResource(NULL, hResource);
if (!hGlobal)
return 2;

DWORD exeSiz = SizeofResource(NULL, hResource);
if (!exeSiz)
return 3;

void* exeBuf = LockResource(hGlobal);
if (!exeBuf)
return 4;

char tempPath[MAX_PATH] = {};
if (!GetTempPathA(MAX_PATH, path))
return 5;

string fullPath = string(tempPath) + "sum2num.exe";

ofstream outfile(fullPath.c_str(), ios::binary);
if (!outfile.is_open())
return 6;

int res = (outfile.write((char*)exeBuf, exeSiz)) ? 0 : 7;
outfile.close();

if (res == 0) {
system(fullPath.c_str());
}

remove(fullPath.c_str());

return res;
}

Embedding resources in executable using GCC

There are a couple possibilities:

  • use ld's capability to turn any file into an object (Embedding binary blobs using gcc mingw):

    ld -r -b binary -o binary.o foo.bar  # then link in binary.o
  • use a bin2c/bin2h utility to turn any file into an array of bytes (Embed image in code, without using resource section or external images)


Update: Here's a more complete example of how to use data bound into the executable using ld -r -b binary:

#include <stdio.h>

// a file named foo.bar with some example text is 'imported' into
// an object file using the following command:
//
// ld -r -b binary -o foo.bar.o foo.bar
//
// That creates an bject file named "foo.bar.o" with the following
// symbols:
//
// _binary_foo_bar_start
// _binary_foo_bar_end
// _binary_foo_bar_size
//
// Note that the symbols are addresses (so for example, to get the
// size value, you have to get the address of the _binary_foo_bar_size
// symbol).
//
// In my example, foo.bar is a simple text file, and this program will
// dump the contents of that file which has been linked in by specifying
// foo.bar.o as an object file input to the linker when the progrma is built

extern char _binary_foo_bar_start[];
extern char _binary_foo_bar_end[];

int main(void)
{
printf( "address of start: %p\n", &_binary_foo_bar_start);
printf( "address of end: %p\n", &_binary_foo_bar_end);

for (char* p = _binary_foo_bar_start; p != _binary_foo_bar_end; ++p) {
putchar( *p);
}

return 0;
}

Update 2 - Getting the resource size: I could not read the _binary_foo_bar_size correctly. At runtime, gdb shows me the right size of the text resource by using display (unsigned int)&_binary_foo_bar_size. But assigning this to a variable gave always a wrong value. I could solve this issue the following way:

unsigned int iSize =  (unsigned int)(&_binary_foo_bar_end - &_binary_foo_bar_start)

It is a workaround, but it works good and is not too ugly.



Related Topics



Leave a reply



Submit