home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / DAUNINST.ZIP / uDaUnInstaller.pas < prev   
Pascal/Delphi Source File  |  2001-07-27  |  4KB  |  132 lines

  1. unit uDaUnInstaller;
  2.  
  3. {*****************************************************************************
  4.  *
  5.  *  uDAUnInstaller.pas - unInstaller Object
  6.  * Based on the TAppexec component from  Patrick Brisacier and Jean-Fabien Connault.
  7.  *
  8.  *  Copyright (c) 2000-1 Diego Amicabile
  9.  *
  10.  *  Author:     Diego Amicabile
  11.  *  E-mail:     diegoami@yahoo.it
  12.  *  Homepage:   http://www.geocities.com/diegoami
  13.  *
  14.  *  This component is free software; you can redistribute it and/or
  15.  *  modify it under the terms of the GNU General Public License
  16.  *  as published by the Free Software Foundation;
  17.  *
  18.  *  This component is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License
  24.  *  along with this component; if not, write to the Free Software
  25.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  26.  *
  27.  *****************************************************************************}
  28.  
  29. interface
  30.  
  31. uses
  32.   Windows,  SysUtils, registry;
  33.  
  34. type
  35.  
  36.   ENoApplicationName = class(Exception);
  37.   EAppNotInstalled = class(Exception);
  38.   ECouldNotUnInstallException = class(Exception);
  39.  
  40.  
  41.   TDAUninstaller = class
  42.   private
  43.     FApplicationName: String;
  44.   protected
  45.     procedure AppExecute(AppString : STring);
  46.   public
  47.     procedure Execute;
  48.     procedure Uninstall(AppName : String);
  49.  
  50.   published
  51.     property ApplicationName : String read FApplicationName write FApplicationName;
  52.   end;
  53.  
  54.  
  55. implementation
  56.  
  57. procedure   TDAUninstaller.Execute;
  58. begin
  59.   Uninstall(FApplicationName)
  60. end;
  61.  
  62. procedure   TDAUninstaller.UnInstall(AppName : String);
  63. var Reg : TRegistry;
  64.     Key1, Key2 : String;
  65.     AppString : String;
  66. begin
  67.   if AppName = '' then begin
  68.     raise ENoApplicationName.Create('No Application Name defined');
  69.     exit;
  70.   end;
  71.   Reg := TRegistry.Create;
  72.   Reg.RootKey:=HKEY_LOCAL_MACHINE;
  73.   Key1:= 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\'+AppName;
  74.   Key2:= 'SOFTWARE\Microsoft\Windows\Current Version\Uninstall\'+AppName;
  75.  
  76.  
  77.   try
  78.     Reg.OpenKey(Key1,false);
  79.     AppString := Reg.ReadString('UninstallString');
  80.  
  81.  
  82.   except
  83.  
  84.     on ERegistryException do
  85.       try
  86.         Reg.OpenKey(Key2,false);
  87.         AppString := Reg.ReadString('UninstallString');
  88.       except on ERegistryException do
  89.         raise EAppNotInstalled.Create('Application was not installed on this computer');
  90.       end;
  91.  
  92.   end;
  93.   AppExecute(AppString);
  94. end;
  95.  
  96.  
  97. procedure   TDAUninstaller.AppExecute(AppString : String);
  98. var
  99.   InstanceID : THandle;
  100.   buffer: array[0..511] of Char;
  101.   TmpStr: String;
  102.   i: Integer;
  103.   StartupInfo:TStartupInfo;
  104.   ProcessInfo:TProcessInformation;
  105. begin
  106.   TmpStr := AppString;
  107.   StrPCopy(buffer,TmpStr);
  108.   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
  109.   StartupInfo.cb := Sizeof(StartupInfo);
  110.   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  111.     StartupInfo.wShowWindow := SW_SHOWNORMAL;
  112.   if not CreateProcess(nil,
  113.     buffer,                        { pointer to command line string }
  114.     nil,                           { pointer to process security attributes }
  115.     nil,                           { pointer to thread security attributes }
  116.     false,                         { handle inheritance flag }
  117.     CREATE_NEW_CONSOLE or          { creation flags }
  118.     NORMAL_PRIORITY_CLASS,
  119.     nil,                           { pointer to new environment block }
  120.     nil,                           { pointer to current directory name }
  121.     StartupInfo,                   { pointer to STARTUPINFO }
  122.     ProcessInfo) then begin        { pointer to PROCESS_INF }
  123.     raise ECouldNotUnInstallException.Create('Could not uninstall');
  124.   end else begin
  125.     WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
  126.     { GetExitCodeProcess(ProcessInfo.hProcess, ErrNo); }
  127.   
  128.   end;
  129. end;
  130.  
  131. end.
  132.