Using C++ OOP for design of TSR (Terminate-and-Stay-Resident) Programs for MS DOS


/* =======================================================================
   This example shows very simple interface to a C++ class that allows
   to create any TSR-Program for MS DOS using OOP principles and methods.
   The Programs based on the 'TSR' class demonstrate highest savety
   and reliability by persistent exploitation since 1994 on 
   THE INTERBANK COMPUTER CENTER OF NATIONAL BANK OF REPUBLIC OF BELARUS.
   
   ©1994-1996 Victor.
   =======================================================================*/
#include "tsr_prep.h"	// macro TSR_PREPARE 
#include "tsr.h"	// class TSR
			// view "tsr_2f.asm"

/* -----------------------------------------------------------------------
   Sets sizes of heap and stack.
   Must be first executive operator in program.
   -----------------------------------------------------------------------*/
TSR_PREPARE()


/* -----------------------------------------------------------------------
   Description of function that will be run by unloading your TSR.
   Sometimes it is needed if your TSR (exactly, your work function) 
   uses and affects other TSR loaded in memory or some systems tools 
   and it is necessary to restore them to status before loading 
   your TSR-Program.
   If no than the "done" function can be simply {}.	  
   -----------------------------------------------------------------------*/
int done(void)
{
}


/* -----------------------------------------------------------------------
   Description of function that does all useful items you design 
   your TSR-program for.
   This function will be run when your TSR-Program will get control:
   - at hot-key pressing
   - at timer event (regularly)
   - at any event that set flag your work function is able to read.
   -----------------------------------------------------------------------*/
int work(void)
{ // Some actions and operations 
}


/* -----------------------------------------------------------------------
   Initialization of object of the TSR class - here you fully describe your
   TSR_Program and you are ready to start it.   
   -----------------------------------------------------------------------*/
const Yes=1;
const No=0;
TSR tsr(	"The Name Of My Very Serious Program",
		work,
			/* -------------------------------------------
			   The work function of TSR-Program.
			   If no work function required than than can be NULL
			   ( or work function can be empty: {}).
			   -------------------------------------------*/ 
		done,
			/* -------------------------------------------
			   The done function of TSR-Program
			   If no done function required than can be NULL
			   ( or done function can be empty: {}).
			   -------------------------------------------*/ 
		0xFA,
			/* -------------------------------------------
			   0xF0...0xFF - The Identificator of this Instance
			   of TSR-Program. This ID must be unique number 
			   for each Instance and is needed for Int 2F
			   handler for TSR control - testing previously loaded
			   program, unloading, activating and desactivating
			   program in memory.
                           See reference about Int 2f for details.
			   -------------------------------------------*/ 
		0x1F,      //  'S'
		CTRL,      //  Ctrl
			/* -------------------------------------------
			   Two parameters define "Hot-Key" for this TSR:
                           - Scan-Code for Key
                           - Code for Controls-Keys Status.
                           See reference about BIOS Kbd for details.
			   If no "Hot-Key" required than
			   Scan-Code for Key must be 0.
			   -------------------------------------------*/ 
		0,
			/* -------------------------------------------
			   The timer period (in CLK_TCK) for regular run 
			   of "work" function.
                           If 0 then timer will not be used for activation.
			   -------------------------------------------*/ 
		Yes,
			/* -------------------------------------------
			   To use or not to use Int 28 for run of "work" 
			   function.
			   Using of Int 28 causes fastest search
		           for suitable moment for TSR's response
			   after "Hot-Key" pressing or timer event,
			   but, theoretically, decreases reliability
			   of TSR program. 
			   -------------------------------------------*/ 
		Yes
			/* -------------------------------------------
			   If program will make disk operations 
		           (open, read, write files etc.)
			   then must be Yes - so tsr object will capture 
		           and correct work with BIOS HDD Interrupt 13. 
			   -------------------------------------------*/ 
        );



/* -----------------------------------------------------------------------
   Main function reads command line string and call one of the methods
   of object of the TSR class.
   That's all.
   -----------------------------------------------------------------------*/
int main( int argc, char *argv[] )
{
  puts(tsr.is_name());
  switch(tsr.is_cmdl(argc,argv))
  {
    case CL_BADKEY	: puts("Unknown parameter.");
    case CL_HELP	: puts("Usage: PGMFILE.EXE /Key\n"
			       " /Z - load\n"
			       " /L - unload\n"
			       " /S - desactivate but leave in memory\n"
			       " /A - activate\n"
			       " /H - help\n");
			  return 0;
    case CL_UNLOAD	: if(!tsr.already_loaded())
			  {
			    puts("Not loaded previously");
                            return 0;
                          }
			  else
			    return tsr.leave();
    case CL_SLEEP	: if(!tsr.already_loaded())
			  {
			    puts("Not loaded previously");
                            return 0;
                          }
			  else
			    return tsr.sleep();
    case CL_AWAKE	: if(!tsr.already_loaded())
			  {
			    puts("Not loaded previously");
                            return 0;
                          }
			  else
			    return tsr.awake();
    case CL_KEEP	: if(tsr.already_loaded())
			  {
			    puts("Already loaded");
                            return 0;
                          }
			  else
			    tsr.keep();
    default		: puts("Unknown error");
                            return 1;
  }
}


If you want to try design your own TSR-Program using this TSR class:

  1. Permission is granted to use this code for any not commercial purpose whatsoever free of charge at your own risk. No warranties on this code are intended or implied.
  2. Download tsrall.zip, unpack it and take as basic example. .

Here is an example of TSR-Program designed using the TSR class. The Program pop-up (small blue window within text and tiny menu) is performed on Ctrl-S key pressing + after each 60 seconds.
The primer includes:

Good Luck!
TopList