unit processLoader1;
{ ****************************************************************
  Source        F:\ProgramFilOPs\RangeA\processloader1.PAS
  Typ:  	Service-Unit, 166 lines
  Autor:  	max kleiner
  Descript:  	copy and starts a process
  Modell:	C:\UML\dllbau\dllplus\proload1.mpb
  Besonderes: 	uses registry, a file and commando-options
  Revisionen:   07.11.00 with buffer-pointer
		02.02.01 commando switches
		23.07.01 MMRedesign and NT-tested
  Ex: file regpath.txt:
  // HKEY_USERS RegKey on server,3.8.01, with destinationpath
  \.DEFAULT\Software\Prosystem\DynaLoader
  commando switch: processloader.exe maxstation myexe      
 **************************************************************** }

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Registry, ComCtrls, ExtCtrls;

type
  {:
  }
  TForm1 = class (TForm)
    Edit_DestFileName: TEdit;
    Edit_SourceFileName: TEdit;
    Edit_StartApp: TEdit;
    ProgressBar1: TProgressBar;
    StatusBar1: TStatusBar;
    Timer1: TTimer;
    procedure FormActivate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    procedure LoadandStart_File;
  end;
  
const
  sRegDestPath= 'destinationpath'; // in Registry
  sRegPathFile= 'regpath.txt';  // as a File

var
  Form1: TForm1;
  sServerName, sStartFile,
  sExeStarterfile, sRegpath, sDestPath,
  sSourceFileName, sDestFileName: string;

implementation

{$R *.DFM}

{:
}
{
***************************************** TForm1 *****************************************
}
procedure TForm1.FormActivate(Sender: TObject);
var
  F: TextFile;
begin
  if ParamCount >= 2 then begin
    sServerName:= ParamStr(1);
    sStartFile:= ParamStr(2);
    Edit_SourceFileName.Text:= sStartFile;
    AssignFile(F, sregpathFile);
    Reset(F);
    Readln(F);              // first line as header
    Readln(F, sregpath);    { Read the second line out}
    CloseFile(F);
    if ParamCount > 3 then begin
      sExeStarterfile:= ParamStr(3);
      Edit_StartApp.Text:= sExeStarterfile;
    end else sExeStarterfile:= '';
  end else begin
    MessageDlg('Mindestens 2 Argumente übergeben', mtError, [mbOk], 0);
    close;
  end;
end;

procedure TForm1.LoadandStart_File;
  
  const   cDataSize = 2048;
  var     Reg: TRegistry;
          bLoad: boolean;
          fsSource, fsDestination : TfileStream;
          sExecCall : string;
          pBuffer: Pointer;
          iSize, i: integer;
  
begin
   bLoad:= false;
   Reg := TRegistry.Create;
   Edit_DestFileName.Text:= '???';
   try
     with Reg do begin
       RootKey := HKEY_USERS;
       if (RegistryConnect(sServerName) or
       RegistryConnect('')) then begin
         OpenKey(sRegpath, False);
         sSourceFileName:= ReadString(sStartFile);
         sDestPath:= readString(sRegDestPath);
         Edit_DestFileName.Text := sSourceFileName;
         bLoad:= (Edit_DestFileName.GetTextLen > 0);
         sDestFileName:= sDestPath + ExtractFileName(sSourceFileName);
       end else
           MessageDlg('Rechner '+sServerName +'not found.',mtError,[mbOk],0);
     end; //with
   finally
      Reg.Free;
   end;
   if bLoad then begin
     StatusBar1.SimpleText:= 'registered, now loading...';
     self.Update;
     try  // fmCreate also
     fsSource:= TFileStream.Create(sSourceFileName, fmOpenRead);
     fsDestination:= TFileStream.Create(sDestFileName, fmCreate);
       iSize:= fsSource.Size;
       {fsDestination.CopyFrom (fsSource, fsSource.Size); normal}
       ProgressBar1.Max:= fsSource.Size;
       GetMem(pBuffer, cDataSize);
       try
         i:= 1;
         while i * cDataSize <= iSize do begin
           fsSource.ReadBuffer (pBuffer^, cDataSize);
           fsDestination.WriteBuffer (pBuffer^, cDataSize);
           ProgressBar1.Position:= i * cDataSize;
           inc(i);
         end;
           fsSource.ReadBuffer (pBuffer^, iSize- (i-1)* cDataSize);
           fsDestination.WriteBuffer (pBuffer^, iSize- (i-1)* cDataSize);
       finally
         freeMem(pBuffer);
       end;
     finally
       fsSource.Free;
       fsDestination.Free;
     end;
   end; //bLoad
     StatusBar1.SimpleText:= 'loaded, now starting...';
     self.Update;
     if sExeStarterfile <> '' then
     sExecCall:= sExeStarterfile+ ' '+ sDestFileName
     else sExecCall:= sDestFileName;
     case WinExec(PChar(sExecCall), SW_SHOWDEFAULT) of
       0: ShowMessage('The system is out of memory or resources.');
       ERROR_BAD_FORMAT: ShowMessage('The .EXE file is invalid in image).');
       ERROR_FILE_NOT_FOUND: ShowMessage('The specified file was not found.');
       ERROR_PATH_NOT_FOUND: ShowMessage('The specified path was not found.');
     end;
    statusBar1.simpleText:= 'started';
  self.Close;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.enabled:= false;
  LoadandStart_File;
end;


end.


