I need to add a binary code of one of my c program to another binary from a c program.how do I do it? I need to add the binary content inside my c source.and use that c source to append that binary content to an existing binary.
-
6This could be an [XY Problem](http://xyproblem.info/). You should [edit] your question and explain a bit more what you are _actually_ trying to achieve. – Jabberwocky May 02 '18 at 06:51
-
At the very least, it's unclear whether you need the binary as data for some processing or you want to use its functionality, therefore **link** it. Yes, edit this question please. – May 02 '18 at 06:57
-
Your issue might not be C specific, you might want to look at https://stackoverflow.com/a/4158997 – dvhh May 02 '18 at 08:20
2 Answers
On Unix-likes (including Linux), you can use xxd:
xxd -i program
This generates a header file with the contents of the file as a char array, embedding it in your program.
On Windows, you can embed the program as a resource. In your .rc
, using 200 as a resource ID:
200 RDCDATA "program.exe"
Then in your program:
HANDLE resinfo = FindResource(instance, MAKEINTRESOURCE(200), RT_RCDATA);
HANDLE reshandle = LoadResource(instance, resinfo);
void *data = LockResource(reshandle);
DWORD datasz = SizeOfResource(instance, resinfo);
In either case you'll need to write out the file to some location to run it. Don't forget to set the executable bit on Unix.

- 5,767
- 3
- 22
- 33
I am sure it is possible to instruct the linker that the binary is a resource of the executable, so it will be included in the image.
I am also sure it is possible for the executable to access the resource and write it out, name it as an executable of the OS (Windows/Linux/Mac) and then exec it.
(A complete solution would depend on the OS)

- 25,048
- 4
- 23
- 41