home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / exec.swg / 0030_Example of calling One program from anot.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1995-02-28  |  2.9 KB  |  123 lines

  1. {
  2.   This code is provided as is, without guarrantees or support of any kind. 
  3.   We have two programs, one of which launches another, and passes
  4.   shared data to it. You can place anything in that shared data,
  5.   including instructions on when to launch a third or fourth program. 
  6.   The shared data could be of virtually any size, the record we
  7.   chose here was picked more or less at random.
  8. }
  9. {$M 1024, 0, 0}
  10. {$R+,S+}
  11. program Home;
  12. Uses
  13.   Dos,
  14.   SharInfo;
  15.  
  16. Const
  17.   SD: TData =
  18.     (S: 'Hi, this message came from Home.Exe.';
  19.     I: 42);
  20.  
  21. var
  22.   AddrStr: String[11];
  23.   Temp: String[5];
  24.  
  25.   procedure HandleInput;
  26.   var
  27.     vSeg, vOfs, Code: Word;
  28.     PData: ^TData;
  29.   begin
  30.     Val(ParamStr(1), vSeg, Code);
  31.     Val(ParamStr(2), vOfs, Code);
  32.     PData := Ptr(vSeg, vOfs);
  33.     WriteLn('Home hears: ', PData^.S);
  34.     WriteLn('The magic number is: ', PData^.I);
  35.   end;
  36.  
  37. begin
  38.   FillChar(AddrStr[1], 11, #32);
  39.   Str(Seg(SD), AddrStr);
  40.   Str(Ofs(SD), Temp);
  41.   Move(Temp[1], AddrStr[length(AddrStr) + 2], length(Temp));
  42.   Inc(AddrStr[0], succ(length(temp)));
  43.   WriteLn('===============');
  44.   WriteLn('Execing Visitor');
  45.   WriteLn('===============');
  46.   Swapvectors;
  47.   Exec('Visitor.Exe', AddrStr);
  48.   Swapvectors;
  49.   WriteLn('==========================');
  50.   WriteLn('We have returned to home. ');
  51.   WriteLn('==========================');
  52.   WriteLn;
  53.   WriteLn('Home Says: ', SD.S);
  54.   WriteLn('Here''s a number visitor gave us: ', SD.I);
  55. end.
  56.  
  57. {======================}
  58. {$M 2024, 0, 2000}
  59. {$S+,R+}
  60. program Visitor;
  61. Uses
  62.   Dos,
  63.   SharInfo;
  64.  
  65. var
  66.   vSeg, vOfs, Code: Word;
  67.   PData: ^TData;
  68.  
  69.   procedure ReportError;
  70.   begin
  71.     WriteLn('This program is a subprogram of Home');
  72.     Halt(1);
  73.   end;
  74.  
  75.   procedure SendDataBack;
  76.   var
  77.     AddrStr: String[11];
  78.     Temp: String[5];
  79.     SD: TData;
  80.   begin
  81.     SD.S := 'Hi, this message came from Visitor.Exe.';
  82.     SD.I := 42;
  83.     FillChar(AddrStr[1], 11, #32);
  84.     Str(Seg(SD), AddrStr);
  85.     Str(Ofs(SD), Temp);
  86.     Move(Temp[1], AddrStr[length(AddrStr) + 2], length(Temp));
  87.     Inc(AddrStr[0], succ(length(temp)));
  88.     Exec('Home.Exe', AddrStr);
  89.   end;
  90.  
  91. begin
  92.   if ParamCount <> 2 then ReportError;
  93.   Val(ParamStr(1), vSeg, Code);
  94.   if Code <> 0 then ReportError;
  95.   Val(ParamStr(2), vOfs, Code);
  96.   if Code <> 0 then ReportError;
  97.   PData := Ptr(vSeg, vOfs);
  98.   WriteLn;
  99.   WriteLn('Visitor hears: ', PData^.S);
  100.   WriteLn;
  101.   PData^.S := 'This is a message from visitor. ';
  102.   PData^.i := 231;
  103.   {SendDataBack;}
  104. end.
  105.  
  106. {======================}
  107.  
  108. Unit SharInfo;
  109. {
  110.   Here 's the data being shared between the two programs. 
  111.   I've declared a record with a string and an integer, but
  112.   it wouldn't matter what the contents of this record 
  113.   happened to be. The fields could be of virtually any type
  114.   and could contain any type of data.
  115. }
  116. Interface
  117. Type
  118.   TData = Record
  119.     S: String;
  120.     I: Integer;
  121.   end;
  122. Implementation
  123. end.