Data Storage in Obejct files - Problem

Hello to everybody,

I know C/C++ for quite a while now and it's embarrassing for me not to be able to solve my problem.

I wanted to write a program, that reads data from a linked in binary object, writes to executale and executes it (for now with the "system()" function, maybe Windows API later).

The Program to be executed is a simple HelloWorld-Program. However, if I store a normal text file in my target program, its no problem and the output file IS the same MD5 as the input. I link the file with the command "ld -r -b binary -o HelloWorld.o HelloWorld.exe" (-r for relocatable, -b binary for explicit binary, and the rest is input and output.

My code is therefore:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
extern char binary_HelloWorld_exe_start;
extern char binary_HelloWorld_exe_end;

int main()
{
    cout << "#Stream started" << endl;

    ofstream file1;
    file1.open("HelloWorld.exe");
    for(char *p = &binary_HelloWorld_exe_start; p < &binary_HelloWorld_exe_end; p++)
    file1 << *p;
    file1.close();

    cout << "#File written" << endl;

    system("HelloWorld.exe");
    return 0;
}


I really hope someone to tell me what i am doing wrong. The mistake is surely quite easy, that I would never mind ;)

Thanks in advance,


Narwaro


EDIT: The real error is: Windows cant execute the copied program, because unsupported 16-bit Application, what isnt the real thing, I think something went wrong with a header, etc., However, the output file is some ~200 bytes larger than the original "HelloWorld.exe".
Last edited on
Not really sure what you want to do, but:
1 -open the file in binary mode:
file1.open("HelloWorld.exe", std::ios::binary);
2 . use write() member function instead of <<operator.


If you just want to store arbitrary data in an already WORKING exe file, write the content at the end of it and is guaranteed to be valid. This is used by self-extracting archivers.
Last edited on
Thanks, I knew it was a stupid mistake, it was just to open the file in binary.
Topic archived. No new replies allowed.