home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 June
/
SIMTEL_0692.cdr
/
msdos
/
pcmag
/
chain_4.arc
/
CHAIN.DOC
< prev
next >
Wrap
Text File
|
1987-11-17
|
9KB
|
186 lines
Chain Facility for Turbo Pascal 4.0
Version 1.0
11/17/87
Kim Kokkonen
Overview
------------------------------------------------------------------------------
Turbo Pascal 4.0 no longer supports several features that many programmers
have come to count upon: Chain, Execute, and overlays. This Chain facility
provides a reasonable facsimile of the Turbo 3 Chain and Execute commands.
Turbo 4's smart linker raises some new issues, though: see the Restrictions
and Limitations section below before getting your hopes too high.
The chain facility is implemented in a small unit that you USE in your
Turbo Pascal 4.0 program. The unit exports a single function called Chain4
which you call to chain to a new program. You can chain to any other EXE or
COM file. Unlike Turbo 3, there is no restriction that the chained program be
another Turbo Pascal program. The new program overwrites the current program
in memory. Control will not return to the original program unless you chain
back to it.
Usage
------------------------------------------------------------------------------
The source code for a unit named CHAIN is provided. Add this unit near the
beginning of your USES statement. CHAIN depends on no other units, and uses
about 400 bytes of code space. The first time you compile it, you'll need
CHAIN.PAS and CHAIN.OBJ. Thereafter, you'll just need to have CHAIN.TPU to
link into your program.
CHAIN exports a single function, Chain4. It is declared as follows:
function Chain4(Path, CmdLine : string) : word;
The Path parameter to Chain4 specifies the name of the new program to execute.
Path must be a complete program name, including the extension, and a drive or
directory name if the file is not in the current directory. CmdLine is the
equivalent of a DOS command line to pass to the new program. Due to internal
limitations of CHAIN, the command line is limited to 95 characters maximum.
Any longer CmdLine will be truncated to 95 characters.
If chaining occurs successfully, the function will not return. If an error
occurs, Chain4 returns a DOS error code. The following error codes are those
most likely to occur:
2 File not found
30 Read fault
Beyond a certain point, Chain4 is committed to chaining and cannot return to
the calling program even if an error occurs. In this case, it will simply halt
and return control to DOS. The only such case we know occurs when insufficient
memory is available to load the new program. If the chained program is
significantly larger than the original, it would be wise to assure that
sufficient memory exists before attempting to chain.
Here are some example calls to Chain4:
Status := Chain4('MENU.EXE', '');
Chains to MENU.EXE in the current directory, passing it an empty command line.
Error information, if any, is returned in the word variable Status.
Status := Chain4('C:\BIN\TPC.EXE', 'MYPROG /M /Q /$T+');
Chains to the command line Turbo compiler, telling it to compile the program
MYPROG.PAS with various options.
Restrictions and Limitations
------------------------------------------------------------------------------
CHAIN works by using DOS function 4B, subfunction 03 (load overlay) to
overwrite the current code in memory and transfer control to the new program.
While performing the load overlay call, CHAIN executes a number of steps:
o All available memory is allocated to the process.
o All file handles except StdIn, StdOut, and StdErr are closed.
o Interrupt vectors 0, 2, 23h, 24h, and 75h taken over by the Turbo SYSTEM
unit are restored to their original values.
o The new command line is put in place within the PSP.
o FCB's normally initialized by the DOS loader are initialized using the new
command line.
o The machine stack is moved to the top of available memory, a step required
for the load overlay call to work reliably. The newly loaded program will
move the stack back to wherever its normal location.
o After the new program is loaded, the registers DS, ES, SS, and SP are
initialized the same way that the DOS loader normally would and control is
transferred to the entry point of the program.
CHAIN cannot handle items on the following list. It is your responsibility to
take any needed action.
o CHAIN does not fiddle with the DOS standard file handles. If standard
input or output are redirected, this will be passed on to the new program.
This may be desirable. If not, the original program should reset these
handles before chaining.
o Interrupt vectors taken over by units other than the SYSTEM unit must be
restored. In particular, The CRT unit takes over interrupt 1Bh. You can
restore it by using the DOS unit in your program, and executing the
following statement prior to chaining: SetIntVec($1B, SaveInt1B);
o Allocation changes to the main memory block (normally handled by the
DOS EXE loader) are not performed. The new process will have all available
memory when it starts up.
Because the Turbo 4 runtime library is not completely incorporated into
programs as it was in Turbo 3, it is not so easy to share data between chained
programs. It is very likely that the new program will overwrite the data
segment of the old. Without playing further tricks, about the only information
that can be passed from the original program to the new program is the DOS
command line.
The trick to share data, not supplied here, can nevertheless be described
fairly easily. The original program should shrink its initial allocated memory
block (using DOS function 4Ah) so that sufficient free memory exists for the
shared data region. Then the original program should allocate an extra block
of memory (using DOS function 48h) to hold the shared data. Function 48h will
return a pointer to the new block. The original program should initialize this
block as desired, and then pass a pointer to it via the DOS command line. The
chained program can read the pointer from the command line, and then safely
refer to the shared block by using the pointer. At some time before the
application finally quits to DOS, it must deallocate the shared data block
(using DOS function 49h). Properly implemented, this technique does not
conflict with the stack movement performed by CHAIN itself.
Chain4 is written completely in assembly language, using MASM 5.0. It will
assemble using MASM 4.0 or later.
Copyright and Acknowledgement
------------------------------------------------------------------------------
The Chain Facility for Turbo Pascal 4.0 is copyright (c) 1987 by TurboPower
Software. All rights reserved.
TurboPower Software hereby grants a limited license to use this software as
follows:
o The CHAIN unit and its source code may be distributed freely as long as
there is no charge, with the exception of a handling fee not to exceed $10.
o Programs using the CHAIN unit may be distributed without restriction,
commercially or otherwise.
Thanks to Ray Lambert, who showed that this function could be done in Turbo
3.0, which buoyed the morale during a few days of machine crashes while trying
to make this one work.
Contact Kim Kokkonen at Compuserve account 72457,2131 with comments regarding
the Chain facility.
Advertisement
------------------------------------------------------------------------------
TurboPower Software is in the business of providing powerful tools for the
Turbo Pascal programmer. Our line of products for Turbo Pascal 3.0 includes
the TurboPower Utilities, Turbo Extender, T-DebugPLUS, and Turbo Optimizer.
Our first product for Turbo Pascal 4.0 is Turbo Professional 4.0, a library of
400 routines to solve the most common programming problems. Professional 4.0
includes units to save time and effort in the following ways:
o CRT unit emulation, with many added features: cursor manipulation, support
for Taskview and Desqview multitaskers, screen save and restore, and more.
o Popup windows, virtual screens, and pulldown menu systems.
o Easy and reliable ways to make your program memory resident.
o Interrupt service routine handlers.
o Keyboard macros.
o Data entry routines.
o Extended and Expanded memory access.
o A complete BCD arithmetic unit, including the transcendental functions and
the Form routine.
o Sorting and searching.
o Extensive string manipulation.
o Strings longer than 255 characters.
o Arrays larger than 64K.
o Runtime error recovery.
Turbo Professional 4.0 also comes with a dozen powerful demo programs,
including a memory resident programmer's calculator, time management system,
keyboard macro utility, and a source code generator for menu systems. With
complete source code (over 30,000 lines of it) and a 450 page manual, Turbo
Professional 4.0 is a bargain at $99.
Coming soon are an overlay manager for Turbo Pascal 4.0, and new versions of
TDEBUG and TurboPower Utilities for the new compiler.
Call TurboPower Software at 408-438-8608 for more information.