home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / ATOTPU.ZIP / AUTOINST.PAS next >
Encoding:
Pascal/Delphi Source File  |  1988-05-06  |  3.9 KB  |  121 lines

  1. program AutoInst2;
  2.  
  3.   { ==================================================================
  4.  
  5.                             Program: AutoInst v3.0
  6.                             Authors: David Dubois
  7.                                      Zelkop Software
  8.                                      Halifax, Nova Scotia
  9.                CompuServe User I.D.: 71401,747
  10.  
  11.                                   and
  12.  
  13.                                      David Doty
  14.                                      Skipjack Software
  15.                                      Columbia, Maryland
  16.                CompuServe User I.D.: 76244,1043
  17.  
  18.                   Date last revised: 1988.05.06
  19.  
  20.     ==================================================================
  21.  
  22.     We hereby dedicate this code to the public domain. We would
  23.     appreciate, though, if you mentioned our names if you use the unit.
  24.  
  25.     This program demonstrates how to write a program which will change
  26.     the value of a typed constant in its own .EXE file.  When the
  27.     program is run again, the data will be initialized to the new
  28.     value. No external files are necessary.
  29.  
  30.     Unlike version 1 of this program, this code will write to typed constants
  31.     defined in a unit.  Unlike version 2, the routine to write to the
  32.     executable is in a unit and the routine will find the name and path of the
  33.     executable- no fixed file name or assumptions about the path!
  34.  
  35.     The code shown here is designed for Turbo Pascal version 4.0.
  36.     There is a simpler method available for Turbo 3.0.
  37.  
  38.     For this example writes a certain string a certain number of times.  It
  39.     then prompts the user for a new string and a new number.  The .EXE file is
  40.     updated to reflect the values entered by the user.  The next time the
  41.     program is run, the new values are used.
  42.  
  43.     USES
  44.  
  45.     Examples of the usefulness of this technique would be:
  46.  
  47.     o  A program that allows the user to change default display colors.
  48.  
  49.     o  A program that keeps track of a password that the user can change.
  50.  
  51.     HOW TO USE THIS TECHNIQUE
  52.  
  53.     Place the WritExec unit in the USES statement of the program or unit that
  54.     needs to write the constant and make the appropriate call to
  55.     WriteToExecutable.
  56.  
  57.     HOW IT WORKS
  58.  
  59.     See the source for the WritExec unit for a detailed description of the
  60.     technique.  Note that this technique will only work for typed constants,
  61.     and the unit will only work under DOS 3.1 or higher (in order to find the
  62.     name and path of the executable, the unit relies on DOS storing this after
  63.     the environment, which only happens under DOS 3.X).
  64.  
  65.     ERROR CHECKING
  66.  
  67.     The unit does some basic error checking (of the IOResult function), and
  68.     returns an error code if something went wrong.
  69.  
  70.     LIMITATIONS
  71.  
  72.     You cannot use MicroSoft's EXEPACK on the .EXE file, or any other
  73.     packing method I know of. This may change the position, or even
  74.     the size of the typed constant in the file image.
  75.  
  76.  
  77.     ==================================================================}
  78.  
  79. uses WritExec;
  80.  
  81. type
  82.   InstallBlockType = record
  83.                        TheNumber : integer;
  84.                        TheString : string;
  85.                      end;
  86.  
  87. const
  88.   InstallBlock : InstallBlockType
  89.  
  90.                = ( TheNumber : 3;
  91.                    TheString : 'Greetings from Zelkop Software' );
  92.  
  93. var
  94.   I : integer;
  95. begin
  96.   with InstallBlock do
  97.     begin
  98.  
  99.       { write out old data }
  100.  
  101.       for I := 1 to TheNumber do
  102.         writeln ( TheString );
  103.  
  104.       { read in new data }
  105.  
  106.       write  ( 'Enter TheNumber: ' );
  107.       readln ( TheNumber );
  108.       write  ( 'Enter TheString: ' );
  109.       readln ( TheString );
  110.     end;
  111.  
  112.   { update .EXE file }
  113.  
  114.   if WriteToExecutable( InstallBlock, SizeOf( InstallBlock ) ) <> 0
  115.   then writeln( 'Error writing to executable file' )
  116.   else writeln( 'New values written to executable file' );
  117.  
  118.   writeln ( 'Now run this program again.' );
  119. end.
  120.  
  121.