Happy Day.

Here's a (somewhat wordy) routine to run GBAK from within a Delphi app.

Good things about it:
- it waits for the GBAK to finish before it comes back.
- it works just like command line GBAK.

Bad things about it:
- it makes assumptions about the registry and the location of GBAK (could 
use FindFile to get around this).
- it creates a temporary batch file so that it can use COMMAND.COM's -e 
option to get extra environment space for the env vars isc_user and 
isc_password (this is no longer necessary - GBAK could be run directly with 
username and password parameters).
- It needs to know whether you're running NT or Win95 (see the first line). 
 This wouldn't be necessary if I eliminated the temporary batch file stuff.

It's workable, but you might want to clean it up a bit.  I know I do.  :)
Cheers.
                        BJ...

< code snippet >

procedure TDMLibExp.RunGBAK (SourceFileName, TargetFileName, Options: 
string);
  function FinalBackslash(const s: string): string;
  begin
    if Copy(S, Length(s) - 1, 1) <> '\' then
      Result := s + '\'
    else
      Result := s;
  end;

var
  InterbaseBinDir: string;
  WinTempDir: string;
  BatchFileName: string;
  BatchLogFileName: string;
  BatchFile: TextFile;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  CreatedOK: boolean;
  status: integer;
  reg: TRegistry;
  ev: TEvent;
  CommandCom, executable: string;
begin
  CommandCom := 'COMMAND.COM';  // Win95
  CommandCom := 'CMD';                    // WinNT

  { Find the windows temp directory.  First, make sure there's enough space 
in
    the variable WinTempDir to accomodate a really long path. }
  WinTempDir := ' 
                                                          ';
  WinTempDir := WinTempDir + WinTempDir + WinTempDir + WinTempDir + 
WinTempDir;
  GetEnvironmentVariable(Pchar('TEMP' + #0), PChar(WinTempDir), 
Length(WinTempDir));
  if WinTempDir = '' then
    WinTempDir := 'c:\'
  else
    WinTempDir := FinalBackslash(Copy(WinTempDir, 1, Pos('' + #0, 
WinTempDir) - 1));

  BatchFileName := WinTempDir + 'IBGBAK.BAT';
  BatchLOGFileName := WinTempDir + 'IBGBAK.TXT';

  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_LOCAL_MACHINE;
     if reg.OpenKey('SOFTWARE\InterBase Corp\InterBase\CurrentVersion', 
False) then
    begin
      InterbaseBinDir := reg.ReadString('ServerDirectory');
      if InterbaseBinDir = '' then
        InterbaseBinDir := reg.ReadString('RootDirectory');
    end
    else if reg.OpenKey('SOFTWARE\Borland\Interbase\CurrentVersion', False) 
then
      InterbaseBinDir := reg.ReadString('RootDirectory');
    reg.CloseKey;


  if InterbaseBinDir <> '' then
  begin
    { Create the batch file that will run Interbase's GBAK. }
    {@stb}
    if (UpperCase(Copy(InterbaseBinDir, Length(InterbaseBinDir) - 3, 4)) <> 
'BIN\') and
       (UpperCase(Copy(InterbaseBinDir, Length(InterbaseBinDir) - 3, 4)) <> 
'\BIN') then
      InterbaseBinDir := FinalBackSlash(InterbaseBinDir) + 'BIN';
    AssignFile(BatchFile, BatchFileName);
    Rewrite(BatchFile);
    Writeln(BatchFile, 'set isc_user=' + SYS_UID);
    Writeln(BatchFile, 'set isc_password=' + SYS_PWD);
    Writeln(BatchFile, 'path %PATH%;c:\;"' + InterbaseBinDir + '"');
    Writeln(BatchFile, Format('gbak %s "%s" "%s"', [Options, 
SourceFileName, TargetFilename]));
    Writeln(BatchFile, 'exit');
    CloseFile(BatchFile);
    {@ste}
  end;

  { Launch a command process to run the batch file. }
  ev := TEvent.Create(nil, False, False, 'TimeKiller');
  try
    FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
    with StartupInfo do
    begin
      cb := SizeOf(TStartupInfo);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
      wShowWindow := SW_HIDE;
    end;
    { Can't run the batch file directly because we need extra environment 
space
      for the setting of the environment variables, so we use command.com 
with
      the /e parameter to get more space.  We're still running our batch 
file,
      via the /c parameter of command.com.   }
    executable := CommandCom + ' /e:5000 /c"' + BatchFileName +
                               '" >>' + BatchLogFileName;
    CreatedOk := CreateProcess(nil, PChar(executable),
                               nil, nil, False, NORMAL_PRIORITY_CLASS, nil, 
nil,
                               StartupInfo, ProcessInfo);
    if CreatedOK then
    begin
      { Wait around for the process to end so that we can delete the batch 
file. }
      with ProcessInfo do
      begin
        WaitForInputIdle(hProcess, INFINITE);
        GetExitCodeProcess(hProcess, status);
        while status = STILL_ACTIVE do
        begin
          GetExitCodeProcess(hProcess, status);

          { A half second delay here so this process doesn't hog CPU }
          ev.WaitFor(500);
          Application.ProcessMessages;
        end;
        CloseHandle(hThread);
        CloseHandle(hProcess);
      end;
    end; {if CreatedOK }
  finally
    ev.Free;
  end;
end;


----------
From:   Amanda Milham[SMTP:[EMAIL PROTECTED]]
Reply To:       [EMAIL PROTECTED]
Sent:   Tuesday, 20 April 1999 08:45
To:     Multiple recipients of list delphi
Subject:        [DUG]:  Running gbak from delphi

I want to be able to run gbak and gfix from within delphi so that I can 
create a scheduling tool to perform backups and sweeps
of our clients database's during the small hours of the morning. The files 
need to be backed up to a zip disk and I want the
program to run as a tray icon so that in the morning it can pop up a 
message reminding them to change the disk. I am using
Delphi 3 client server (with patch 2 applied).
Any help would be greatly appreciated.

Regards
Amanda Milham
Information Systems Guru

WizBang Limited - delivering tomorrow's technology
Email: [EMAIL PROTECTED]
Phone: +64 9 419 7458 or +64 21 617 878
PO Box 33699, Takapuna, Auckland, New Zealand


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to