Link to home
Start Free TrialLog in
Avatar of snidx2
snidx2

asked on

C++ Combining two files into one executable.

I have been looking around for a long time and havent been able to find anything on combining two files(preferably executables) into one executable file. This output file will be able to run both of the programs inside itself.

I have seen programs that bind a file onto another and execute both of them once run.

Please give me some examples or help.
Avatar of Axter
Axter
Flag of United States of America image

What is the purpose of this requirement?

If you can explain why you're tyring to do this, we could probably give you a better alternative.
Do you have the source code for the executable files?

What OS and what compiler are you trying to do this in?

If you're using Windows VC++, you could try creating a third executable file, that compiles with the two other executables as binary resource data.


Please give us more information so that we can give you a better answer.

1.  What's the purpose of the requirement?
2.  What's your OS?
3.  What's your compiler (include version)?

Avatar of RexNg
RexNg

I have no idea what r u talking about, but I try to think it logically.

1) 2 executable files -> 2 entries points
2) Then combine 2 executable file to a unique executable file -> unique entry point -> there is a mechanism to choose which one of those enties points to start.

In conclusion, the mechanism scheme is in your mind, and u gotta do it on your own, pal!
Avatar of snidx2

ASKER

I am trying to add one file(executable) onto another executable so that they will both run at the same time but as one file.

I do not have the source for the two files. I am hoping to be able to do this with the GNU compiler on a Windows opertaing system.

-Compiler: (bloodshed)Dev-C++ 4.9.7.0
-OS: Windows

I am interested in attaching one file to another. To create my own compression/zip program. To package multiple files into one.
Avatar of snidx2

ASKER

i would like this is to be a console based program inplain ansi c++, if possible.
Consider this.

You have programs a.exe and b.exe ... n.exe zipped or archived in a file or whatever

You can create another program lets call it run.exe

This program will perform:

1- unzip/get exe files from archive and put them in a temporary directory

2- execute each program with its required parameters using for example the system() function.

3- When all programs exit you can clean up the temporary directory if you want. To do so you need to get the status of the programs running !

So it will be like (without a cleanup) something like:

int my_unzip_or_get_files(char * file_names)
{
  // get files from archive and put them in the temp directory
  // in file_names return an array with the exe files with the full path
  // the function return value is the actual number of programs in the array that should be run.
}

int main(int argc, char **argv)
{
  char files_names[10];
  int total_exe_files = my_unzip_or_get_files(char * file_names);
  for (int i=0;i<total_exe_files;++i)
    system(file_names[i]);
 
  return 0;
}

Of course you can use STL if you want/can, like for returning vector<string> with the file names etc. As I don't know your compiler I don't know if STL is supported and how much.
What OS are you using?  This is very OS/linker dependent.  On some OSs, you can extract the object code for each routine from the executable.  You now have a whole bunch of object files for each routine.  All you have to do then is put them back into a library, write a main to call the relevant bits and link it all together.

I'd only do this for C programs: not C++.  The name mangling, templates etc cause no end of problems.
Avatar of snidx2

ASKER

I am using Windows XP

could you give me some examples.. I am not very good at c++ programming, and this is my first attempt at compressing files into one.

Mafalda, I would rather, if possible just have the two programs run, instead of extracting them into a temp dir then running them then deleting them. The idea is to make one final exe program which somehow runs two programs inside of itself.

I am not sure whether you have already thought about this approach or not but I think this just might help. Create a simple C/C++ program with one main section. Depending on the OS platform, you can choose to either fork a new Child Process or create Light-weight Process (for Solaris) or Thread and run one of the executable in main program sequence by using a suitable system utility functions (you can choose between any of the variations of exec..()series like execl(),execle() or execv()) with sutiable parameters and the second executable one in the secondary execution path(i.e, child process/light-weight process/thread). Now build an exe out of this program. This way both of the external execuatbles would run inside this new execuatble and program entry-point issues would also be solved.
You'd usually add the file to extract as a resource of type "BINRES" (cannot give youi a sample on how to do that with DevX, since I never used it). Then, at runtime, you can extract it like

HRSRC   hRsrc;
HGLOBAL hResource;
DWORD   dwSize;    
LPVOID  lpv;
HFILE   hfFile;


hRsrc = FindResource(hInst,MAKEINTRESOURCE(ID_MYEXE),"BINRES"); // ID_MYEXE is the resource ID

hResource = LoadResource(hInst, hRsrc);
dwSize = SizeofResource(hInst, hRsrc);
lpv = LockResource(hResource);
   
// write resource/exe to disk
hfFile = _lcreat("myexe.exe",0);
_hwrite(hfFile, lpv, dwSize);
_lclose(hfFile);
Avatar of snidx2

ASKER

just a little more info:

I am trying to make a program to bind/attach itself to another executable. So that when this "victim" executable if run then the "attacking" executable is run first along side with the "victim" executable.

ill up the point to 1000, if someone can give an example and some reads, thoughts etc.
Avatar of snidx2

ASKER

"You cannot exceed 500 points for any single question." sorry.. only 500 points.. im new
This almost sounds like a virus......

Can you please give more detailed information about your requirements, as to why you need this functionality?
Avatar of snidx2

ASKER

i need this in order to have a securit program like foolproof to be scattered around the hard disk incase any malicious users try to remove it.
ASKER CERTIFIED SOLUTION
Avatar of shailendra23
shailendra23

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snidx2

ASKER

yes shailendra23, thats what i would like to do. does anyone have any examples, help etc?
Avatar of snidx2

ASKER

gimme an example...