[comp.sys.ibm.pc] Modifying .exe files

cac@auc.UUCP (Clayton Collie) (12/06/89)

I've been working on a fairly large project ( written with TP 5.5 )  
and I'm currently trying to devise a configuration routine (or file) for
the program. I'm curious to find out if anyone on the net has any experience
writing configuration programs that modify the actual program executable  
e.g. the Tinst.exe utility that comes with the Turbo package.
 
  As far as I've gotten is to generate a map file listing the program 
variables and constants along with their segments and offsets. I figure that by using this information could seek to that particular location in the .exe 
and place the appropriate value at that point. 

  
  Is this incredibly difficult ? Any pointers would be appreciated.
BTW - answers don't necessarily have to be TP (or Pascal) specific.


                                      ----- THANX ------- 
                                      Clayton  Collie

 

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (12/07/89)

You have the right idea on how to modify the contents of an exe, but you
may want to give up a little performance for simplicity and ease of
change by going to an external configuration file.

Commercial programs do both things, pick a paradigm which pleases you
and go for it. Remember that things like exepack and axe modify the file
while leaving it executable, and that may move your target in the image.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

nts0699@dsac.dla.mil (Gene McManus) (12/08/89)

From article <32336@auc.UUCP>, by cac@auc.UUCP (Clayton Collie):
   [ ... ]
> the program. I'm curious to find out if anyone on the net has any experience
> writing configuration programs that modify the actual program executable  
> e.g. the Tinst.exe utility that comes with the Turbo package.
>  
> 
>   
>   Is this incredibly difficult ? Any pointers would be appreciated.
> BTW - answers don't necessarily have to be TP (or Pascal) specific.
> 
> 
>                                       ----- THANX ------- 
>                                       Clayton  Collie
> 
>  

I've used a technique (using M'soft C) that goes something like this:

First, define your configuration data (best in a header file):

   struct _config
      {
      /* whatever is necessary for your configuration data */
      unsigned int checksum;          /* .EXE files must checksum to zero */
      } CONFIG;

Then, to write the initial config data in your .EXE file:

   CONFIG ConfigData, *p;
   int i;
   unsigned int sum;

   /* compute a checksum for the configuration structure so that the
      net sum is zero. This is necessary to ensure that MS-DOS doesn't
      choke on the .EXE file at run time, thinking it's corrupted.
   */
 
   p = &ConfigData;                /* beginning address of structure       */
   for(i = 0, sum = 0; i < sizeof(CONFIG); i++, p++)
      sum += *p;                   /* accumulate sum of bytes in structure */
   ConfigData.checksum = -sum;     /* store negative sum                   */

   handle = open("program.exe", O_APPEND);   /* assumes file exists        */
   write(handle, ConfigData, sizeof(CONFIG));/* adds to end of .EXE file   */
   close(handle);

Finally, to read the config data when your program runs:

   CONFIG ConfigData;

   handle = open(argv[0], O_RDONLY);
   lseek(handle, sizeof(CONFIG), SET_END); /* look this up for correct order */
   read(handle, ConfigData, sizeof(CONFIG));/* read the config data           */
   close(handle);

I can't take any credit for inventing this. The general technique came from
one of the magazines I subscribe to, can't remember which.

Good Luck, regards,
Gene


Gene McManus @ Defense Logistics Agency Systems Automation Center,
               Columbus, OH 43215 (614) 238-9403,    Autovon 850-
Internet:      gmcmanus@dsac.dla.mil  (131.78.1.1)
UUCP:          {uunet!gould,cbosgd!osu-cis}!dsacg1!gmcmanus
The views expressed are my own, not those of the Agency, or Dept. of Defense