C++ Combining two files into one executable.

snidx2
snidx2 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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.

Commented:
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)?

Commented:
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!
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
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.

Author

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

Commented:
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.
cup

Commented:
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.

Author

Commented:
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.

Commented:

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.
jkr
Top Expert 2012

Commented:
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);

Author

Commented:
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.

Author

Commented:
"You cannot exceed 500 points for any single question." sorry.. only 500 points.. im new

Commented:
This almost sounds like a virus......

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

Author

Commented:
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.
I think what you are aiming at some thing(a parasite kind of thing) which will get executed before any access to handle of your host file or in other words you want to get notified when ever a list of files are accessed and you want to have control of allowing/disallowing the access ... i don't exactly know how to achieve this but i think if you can have the handles of all the resources which are currently loaded in the memory and you have the handle to access internals of kernal you can achieve it but don't ask me for an implementation because i don't know.

other way to achieve this is do some interupt handling i.e trap the disk i/o interupt so on every disk access you know what is about to happen.

Author

Commented:
yes shailendra23, thats what i would like to do. does anyone have any examples, help etc?

Author

Commented:
gimme an example...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial