How to Embed/Link Binary Data into a Windows Module

How to Embed/Link binary data into a Windows module

  1. Right click the resource script (.rc file)
  2. Choose Import

http://msdn.microsoft.com/en-us/library/saced6x2.aspx

You can embed any "custom" file you want, as well as things like .bmps and stuff VisualStudio "knows" how to edit. Then you can access them with your framework's resource functions like FindResource LoadResource etc...

If you don't have a resource script.

  1. Click Project
  2. Add New Item
  3. Resource Script

http://msdn.microsoft.com/en-us/library/sxdy04be(v=VS.71).aspx

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.

commandline tool for embedding binary file into obj file

If you are using gcc. You can use the linker to do just that using something like:

ld -r -b binary -o foo.o foo.txt

Take a look here

C++: How to add raw binary data into source with Visual Studio?

The easiest and most portable way would be to write a small
program which converts the data to a C++ source, then compile
that and link it into your program. This generated file might
look something like:

unsigned char rawData[] =
{
0x12, 0x34, // ...
};

How to embed a string in the compiled binary at build time?

Declare a variable in your code (in the example below, in main, it will be set in the next step)

var commit string

Add a flag to the build

go build -ldflags "-X main.commit=$GIT_COMMIT"

You can then access the variable as usual

log.Info().Msgf("commit → %v", commit)

Can I attach files directly to the executable in Rust?

Short answer: You can but depending on use case it might not always be best way to get thing done.

Use include_bytes! or include_str!

  • If it is something like README file, Lisence, json config or something use include_str! macro
  • If it is some binary file like DLL you just mentioned use include_bytes

Keep in mind that:
If you are trying to enclose raw file which is large in size or you are including multiple files, it will brings all the drawbacks of having huge binary file.
Instead you can include the code to download that file if it can be hosted.
Even better, if you are distributing your package as application, you can back everything in AppImage or deb file. I believe windows and mac have something similar.



Related Topics



Leave a reply



Submit