home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / ATOTPU.ZIP / WRITEXEC.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1988-05-08  |  9.9 KB  |  264 lines

  1.  
  2. UNIT WritExec;
  3.   { ==================================================================
  4.  
  5.                                Unit: WritExec
  6.                              Author: David Doty
  7.                                      Skipjack Software
  8.                                      Columbia, Maryland
  9.                CompuServe User I.D.: 76244,1043
  10.  
  11.     This unit is based on a previously published program:
  12.  
  13.                             Program: AutoInst v2.0
  14.                              Author: David Dubois
  15.                                      Zelkop Software
  16.                                      Halifax, Nova Scotia
  17.                CompuServe User I.D.: 71401,747
  18.                   Date last revised: 1988.04.24
  19.  
  20.     ==================================================================
  21.  
  22.     This source code is released to the public domain.  If further changes
  23.     are made, please include the above credits in the distributed code.
  24.  
  25.     This unit allows a program to change the value of a typed constant in its
  26.     own .EXE file.  When the program is run again, the data will be initialized
  27.     to the new value.  No external configuration files are necessary.
  28.  
  29.     USES
  30.  
  31.     Examples of the usefulness of this technique would be:
  32.  
  33.     o   A program that allows the user to change default display colors.
  34.  
  35.     o   A program that keeps track of a password that the user can change.
  36.  
  37.     HOW IT WORKS
  38.  
  39.     You don't have to understand all the details in order to use this
  40.     technique, but here they are.
  41.  
  42.     The data to be changed must be stored in a TurboPascal typed
  43.     constant.  In all effect, a typed constant is actually a pre-
  44.     initialized variable.  It is always stored in the program's Data
  45.     Segment.  The data can be of any type.
  46.  
  47.     First, the procedure finds the .EXE file by examining the DOS command
  48.     line, stored with the copy of the DOS environment for the program.  This
  49.     allows the program to find itself no matter where is resides on disk and
  50.     no matter how its name is changed by the user.
  51.  
  52.     The untyped file is opened with a record size of 1. This allows us
  53.     to read or write a string of bytes using BlockRead and BlockWrite.
  54.  
  55.     As documented in the DOS Technical Reference, the size of the .EXE
  56.     header, in paragraphs (a paragraph is 16 bytes), is stored as a
  57.     two-byte word at position 8 of the file.  This is read into the
  58.     variable HeaderSize.
  59.  
  60.     The next step is to find the position of the typed constant in the
  61.     .EXE file. This requires an understanding of the Turbo Pascal 4.0
  62.     memory map, documented on the first and second pages of the Inside
  63.     Turbo Pascal chapter. (That's chapter 26, pages 335 and 336 in my
  64.     manual.)
  65.  
  66.     First, find the address in memory where the typed constant is
  67.     stored. This can be done in Turbo Pascal by using the Seg and Ofs
  68.     functions. Next find the segment of the PSP (program segment
  69.     prefix). This should always be the value returned by PrefixSeg.
  70.     That will mark the beginning of the program in memory. The
  71.     position of the typed constant in the .EXE image should be the
  72.     number of bytes between these two places in memory. But ...
  73.  
  74.     But, two corrections must be made. First, the PSP is not stored in
  75.     the .EXE file. As mentioned on page 335, the PSP is always 256
  76.     bytes. We must subtract that out. Secondly, there is the .EXE file
  77.     header. The size of this has already been read in and must be
  78.     added in to our calculations.
  79.  
  80.     Once the position has been determined, the data stored in the
  81.     typed constant is written in one fell swoop using a BlockWrite.
  82.     This replaces the original data, so that the next time the program
  83.     is run, the new values will used.
  84.  
  85.     LIMITATIONS
  86.  
  87.     You cannot use MicroSoft's EXEPACK on the .EXE file, or any other
  88.     packing method I know of. This may change the position, or even
  89.     the size of the typed constant in the file image.
  90.  
  91.     NOTES
  92.  
  93.     Since typed constants are always stored in the data segment, the
  94.     function call to Seg( ObjectToWrite ) can be replaced with DSeg. I
  95.     prefer using Seg since it is more descriptive.
  96.  
  97.     One might think that Cseg can used as an alternative to using
  98.     PrefixSeg and subtracting 256. This will work only if the code
  99.     resides in the main program. If, on the other hand, the code is
  100.     used in a unit, PrefixSeg must be used as described here. You
  101.     might as well use PrefixSeg and save yourself some headaches.
  102.  
  103.     If you have any comments or questions we would be glad to hear
  104.     them. If you're on CompuServe, you can EasyPlex a letter to
  105.     76244,1043 or 71401,747. Or leave a message on the Borland Programmer's A
  106.     Forum (GO BPROGA). Or, you can write to
  107.  
  108.                          Skipjack Software
  109.                          P. O. Box 61
  110.                          Simpsonville Maryland 21150
  111.  
  112.                             or
  113.  
  114.                          Zelkop Software
  115.                          P.O. Box 5177
  116.                          Armdale, N.S.
  117.                          Canada
  118.                          B3L 4M7
  119.  
  120.     ==================================================================}
  121.  
  122.  
  123. INTERFACE
  124.  
  125.  
  126. FUNCTION GetExecutableName : STRING;
  127. {  This function returns the full drive, path, and file name of the application
  128.    program that is running.  This function is of more general interest than
  129.    just for writing into the EXE file.
  130.  
  131.    NOTE: THIS FUNCTION WILL ONLY WORK UNDER DOS 3.X!!! }
  132.  
  133.  
  134. FUNCTION WriteToExecutable( { input } VAR ObjectToWrite;
  135.                             { input } ObjectSize : WORD ) : INTEGER;
  136. {  This procedure modifies the EXE file on disk to contain changes to typed
  137.    constants.  NOTE - the object MUST be a typed constant.  It may be found
  138.    in any part of the program (i.e., main program or any unit).  The call is
  139.    made by untyped address, to allow any kind of object to be written.  The
  140.    function returns the DOS error code from the I/O operation that failed
  141.    (if any did); if all operations were successful, the function returns 0. }
  142.  
  143.  
  144. IMPLEMENTATION
  145.  
  146.  
  147. FUNCTION GetExecutableName : STRING;
  148. {  This function returns the full drive, path, and file name of the application
  149.    program that is running.  This function is of more general interest than
  150.    just writing into the EXE file.
  151.  
  152.    NOTE: THIS FUNCTION WILL ONLY WORK UNDER DOS 3.X!!! }
  153.  
  154. TYPE
  155.    Environment = ARRAY[ 0..32766 ] OF CHAR;
  156. CONST
  157.    NullChar : Char = #0;
  158.    SearchFailed = $FFFF;
  159. VAR
  160.    MyEnviron : ^Environment;
  161.    Loop : WORD;
  162.    TempWord : WORD;
  163.    EnvironPos : WORD;
  164.    FilenamePos : WORD;
  165.    TempString : STRING;
  166. BEGIN { FUNCTION GetExecutableName }
  167.  
  168.    { Get pointer to DOS environment }
  169.    MyEnviron := Ptr( MemW[ PrefixSeg:$2C ], 0 );
  170.  
  171.    { Look for end of environment }
  172.  
  173.    EnvironPos := SearchFailed;
  174.    Loop := 0;
  175.    WHILE Loop <= 32767 DO BEGIN
  176.       IF MyEnviron^[ Loop ] = NullChar
  177.       THEN IF MyEnviron^[ Loop + 1 ] = NullChar
  178.          THEN BEGIN { found two nulls - this is end of environment }
  179.             EnvironPos := Loop;
  180.             Loop := 32767
  181.          END { found two nulls };
  182.       Inc( Loop )
  183.    END { WHILE Loop };
  184.    IF EnvironPos = SearchFailed
  185.    THEN GetExecutableName := ''
  186.    ELSE BEGIN { found end of environment - now look for path/file of exec }
  187.  
  188.       EnvironPos := EnvironPos + 4;
  189.       FilenamePos := SearchFailed;
  190.       TempWord := EnvironPos;
  191.       Loop := 0;
  192.       WHILE Loop <= 127 DO BEGIN
  193.          IF MyEnviron^[ TempWord ] = NullChar
  194.          THEN BEGIN { found a null - this is end of path/file of exec }
  195.             FilenamePos := Loop;
  196.             Loop := 127
  197.          END { found a null };
  198.          Inc( Loop );
  199.          Inc( TempWord )
  200.       END { WHILE Loop };
  201.       IF FilenamePos = SearchFailed
  202.       THEN GetExecutableName := ''
  203.       ELSE BEGIN { found executable name - move into return string }
  204.         TempString[ 0 ] := Chr( FilenamePos );
  205.         Move( MyEnviron^[ EnvironPos ], TempString[ 1 ], FilenamePos );
  206.         GetExecutableName := TempString
  207.       END { found executable name }
  208.  
  209.    END { found environment end }
  210.  
  211. END { FUNCTION GetExecutableName };
  212.  
  213.  
  214. FUNCTION WriteToExecutable( { input } VAR ObjectToWrite;
  215.                             { input } ObjectSize : WORD ) : INTEGER;
  216. {  This procedure modifies the EXE file on disk to contain changes to typed
  217.    constants.  NOTE - the object MUST be a typed constant.  It may be found
  218.    in any part of the program (i.e., main program or any unit).  The call is
  219.    made by untyped address, to allow any kind of object to be written.  The
  220.    function returns the DOS error code from the I/O operation that failed
  221.    (if any did); if all operations were successful, the function returns 0. }
  222. CONST
  223.    PrefixSize = 256; { number of bytes in the Program Segment Prefix }
  224. VAR
  225.    Executable : FILE;
  226.    HeaderSize : WORD;
  227.    ErrorCode : INTEGER;
  228. BEGIN { FUNCTION WriteToExecutable }
  229.    Assign( Executable, GetExecutableName );
  230.    {$I-} Reset( Executable, 1 );
  231.    ErrorCode := IOResult;
  232.  
  233.    IF ErrorCode = 0
  234.    THEN BEGIN { seek position of header size in EXE file }
  235.       Seek( Executable, 8 );
  236.       ErrorCode := IOResult
  237.    END { seek header };
  238.  
  239.    IF ErrorCode = 0
  240.    THEN BEGIN { read header size in EXE file }
  241.       BlockRead( Executable, HeaderSize, SizeOf( HeaderSize ) );
  242.       ErrorCode := IOResult
  243.    END { read header };
  244.  
  245.    IF ErrorCode = 0
  246.    THEN BEGIN { seek position of object in EXE file }
  247.       Seek( Executable,
  248.             LONGINT( 16 ) * ( HeaderSize + Seg( ObjectToWrite ) - PrefixSeg ) +
  249.             Ofs( ObjectToWrite ) - PrefixSize );
  250.       ErrorCode := IOResult
  251.    END { Seek object position in file };
  252.  
  253.    IF ErrorCode = 0
  254.    THEN BEGIN { write new password in EXE file }
  255.       BlockWrite( Executable, ObjectToWrite, ObjectSize );
  256.       ErrorCode := IOResult
  257.    END { write new password };
  258.  
  259.    Close( Executable );
  260.    WriteToExecutable := ErrorCode
  261.  
  262. END { FUNCTION WriteToExecutable };
  263.  
  264. END { UNIT WritExec }.