Reply to Message

View discussion in a popup

Replying to:
Cosmin2
Contributor
Contributor

For no 3 - I have made a small program.

It's made in Delphi and the code is:

program remset;

uses
   SHFolder, Windows, Sysutils;

const
   SHGFP_TYPE_CURRENT = 0;

var
   path: array[0..MAX_PATH] of char;
   i, fl: Integer;
   Found: array of string;
   s, Name: AnsiString;
   Error: Boolean = False;
   doAsk: Boolean = False;
   isInstall: Boolean = True;
   DisplayErrorMessages: Boolean = False;

function Ceil(const X: Extended): Integer;
begin
   Result := Integer(Trunc(X));
   if Frac(X) > 0 then
      Inc(Result);
end;

procedure FindAll(DirName: string);
var sr: TSearchRec;
begin
   if DirName[Length(DirName)] <> '\' then
      DirName := Dirname + '\';
   try
      if FindFirst(DirName + '*.*', faDirectory, sr) = 0 then
         repeat
            if (sr.Name <> '.') and (sr.Name <> '..') then
            begin
               Inc(fl);
               if fl > Length(Found) then
                  SetLength(Found, Ceil(1.1 * fl));
               Found[fl - 1] := 'd ' + DirName + sr.Name;
               FindAll(DirName + sr.Name);
            end;
         until FindNext(sr) <> 0;
      FindClose(sr);
   except
   end;

   try
      if FindFirst(DirName + '*.*', faAnyFile, sr) = 0 then
         repeat
            Inc(fl);
            if fl > Length(Found) then
               SetLength(Found, Ceil(1.1 * fl));
            Found[fl - 1] := 'f ' + DirName + sr.Name;
         until FindNext(sr) <> 0;
      FindClose(sr);
   except
   end;
end;

begin
   if ParamCount = 0 then
   begin
      MessageBox(0, 'No parameter set!', 'Error', MB_OK or MB_ICONERROR);
      Exit;
   end;
   for i := 2 to ParamCount do
   begin
      if AnsiLowerCase(ParamStr(i)) = '/ask' then
         doAsk := True;
      if AnsiLowerCase(ParamStr(i)) = '/uninstall' then
         isInstall := False;
      if AnsiLowerCase(ParamStr(i)) = '/verbose' then
         DisplayErrorMessages := True;
   end;
   try
      if not SUCCEEDED(SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, @path[0])) then
      begin
         if DisplayErrorMessages then
            MessageBox(0, 'Problem getting AppData folder name!', 'Error', MB_OK or MB_ICONERROR);
         Exit;
      end;
   except
      Exit;
   end;
   s := ParamStr(1);
   if s[Length(s)] = '\' then
      Delete(s, Length(s), 1);
   s := ExtractFileName(s);
   i := Pos(' (vmware thinapp)', AnsiLowerCase(s));
   if i > 0 then
      s := Copy(s, 1, i - 1);
   if s = '' then
   begin
      if DisplayErrorMessages then
         MessageBox(0, 'Error getting folder name!', 'Error', MB_OK or MB_ICONERROR);
      Exit;
   end;
   if DirectoryExists(path + '\Thinstall\' + s) then
   begin
      if doAsk then
         if isInstall then
         begin
            if MessageBox(0, PChar('Previous settings found in "AppData\Thinstall\' + s + '".'#13#10'Do you want the data to be deleted?'), 'Warning', MB_YESNO or MB_ICONWARNING) <> 6 {mrYes} then
               Exit;
         end
         else
            if MessageBox(0, PChar('Do you want the current settings from  "AppData\Thinstall\' + s + '" to be deleted?'), 'Warning', MB_YESNO or MB_ICONWARNING) <> 6 {mrYes} then
               Exit;
      fl := 0;
      FindAll(path + '\Thinstall\' + s);
      if fl > 0 then
      begin
         for i := fl - 1 downto 0 do
         begin
            Name := Copy(Found[i], 3, Length(Found[i]) - 2);
            try
               SetFileAttributes(PChar(Name), FILE_ATTRIBUTE_NORMAL);
            except
            end;
            try
               if Found[i][1] = 'f' then
                  Error := not DeleteFile(Name)
               else
                  Error := not RemoveDir(Name);
            except
               Error := True;
            end;
         end;
         SetLength(Found, 0);
      end;
      try
         Error := not RemoveDir(path + '\Thinstall\' + s);
      except
         Error := True;
      end;
      if Error and DisplayErrorMessages then
         MessageBox(0, 'Not all the data could be removed...', 'Error', MB_OK or MB_ICONERROR);
   end;
end.

I attached the compiled exe and I will explain how it can be used for those who might find it useful.

Now, using a msi editor (like Orca or InstEd) open template.msi from ThinApp's folder.

Add a row in Binary section with the name remset and the path to the exe file as value.

In CustomAction section add 2 rows:

RemovePreviousSettings    258    remset    "[INSTALLDIR]"

RemoveCurrentSettings    258    remset    "[INSTALLDIR]" /uninstall

In InstEd set 0x0102 instead of 258.

The first is for installation, the second is for uninstallation.

If you want to be asked if it's ok to delete add /ask as a parameter at Target.

In InstallExecutteSequence add these rows:

RemoveCurrentSettings    REMOVE    2000

RemovePreviousSettings    NOT Installed OR REINSTALL    2000

Now save the changes made to the msi file and close the editor.

Rebuild the application(s) with relink or build new one(s).

Feedback is appreciated - to help improve it...

Reply
0 Kudos