You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Uninstaller.dpr 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. program Uninstaller;
  2. {$MODE Delphi}
  3. {$APPTYPE GUI}
  4. uses Windows, SysUtils, classes, registry;
  5. {$R uninstall.res}
  6. function GetTempDirectory(): String;
  7. var
  8. buf: array[0..MAX_PATH] of Char;
  9. wintemp, temp: String;
  10. begin
  11. GetTempPath(SizeOf(buf)-1, buf);
  12. wintemp := StrPas(buf);
  13. Randomize;
  14. temp := '\DMDirc-uninstaller-'+inttostr(1000 + Random(1000));
  15. while (DirectoryExists(wintemp+temp+'\')) do begin
  16. temp := temp+'-'+inttostr(1+Random(1000));
  17. end;
  18. MkDir(wintemp+temp+'\');
  19. result := wintemp+temp+'\';
  20. end;
  21. // Run an application and don't wait for it to finish.
  22. function Launch(sProgramToRun: String; hide: boolean = false): TProcessInformation;
  23. var
  24. StartupInfo: TStartupInfo;
  25. begin
  26. FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  27. with StartupInfo do begin
  28. cb := SizeOf(TStartupInfo);
  29. dwFlags := STARTF_USESHOWWINDOW;
  30. if hide then wShowWindow := SW_HIDE
  31. else wShowWindow := SW_SHOWNORMAL;
  32. end;
  33. CreateProcess(nil, PChar(sProgramToRun), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, Result);
  34. end;
  35. // Run an application and wait for it to finish.
  36. function ExecAndWait(sProgramToRun: String; hide: boolean = false): Longword;
  37. var
  38. ProcessInfo: TProcessInformation;
  39. begin
  40. ProcessInfo := Launch(sProgramToRun, hide);
  41. getExitCodeProcess(ProcessInfo.hProcess, Result);
  42. while Result=STILL_ACTIVE do begin
  43. sleep(1000);
  44. GetExitCodeProcess(ProcessInfo.hProcess, Result);
  45. end;
  46. MessageBox(0, PChar('Exec: '+sProgramToRun+#13#10+'Res: '+inttostr(Result)), 'DMDirc Uninstaller', MB_OK + MB_ICONEXCLAMATION);
  47. end;
  48. function KillDir(Dir: string): Integer;
  49. var
  50. searchResult: TSearchRec;
  51. begin
  52. Result := 0;
  53. if FindFirst(Dir+'\*', faDirectory + faHidden + faReadOnly + faSysfile + faAnyFile, searchResult) = 0 then
  54. begin
  55. repeat
  56. if (searchResult.attr and faDirectory) <> faDirectory then begin
  57. Try
  58. DeleteFile(Dir+'\'+searchResult.name);
  59. Except
  60. MessageBox(0, PChar('Unable to delete "'+Dir+'\'+searchResult.name+'" - is DMDirc still running?.'), 'DMDirc Uninstaller', MB_OK);
  61. end;
  62. end
  63. else begin
  64. if (searchResult.name <> '.') and (searchResult.name <> '..') then begin
  65. KillDir(Dir+'\'+searchResult.name);
  66. end;
  67. end;
  68. until FindNext(searchResult) <> 0;
  69. FindClose(searchResult);
  70. end;
  71. Try
  72. RmDir(Dir);
  73. Except
  74. end;
  75. end;
  76. var
  77. TempDir: String;
  78. InstallDir: String = '';
  79. hK32: THandle;
  80. i: Integer;
  81. Reg: TRegistry;
  82. handlerInfo: String;
  83. profileDir: String;
  84. deleteProtocol: boolean;
  85. begin
  86. if MessageBox(0, PChar('This will uninstall DMDirc.'+#13#10+#13#10+'Do you want to continue?'), 'DMDirc Uninstaller', MB_YESNO) = IDYES then begin
  87. if (ParamCount > 0) then begin
  88. for i := 1 to ParamCount do begin
  89. InstallDir := InstallDir+' '+paramstr(i);
  90. end;
  91. InstallDir := trim(InstallDir);
  92. KillDir(InstallDir);
  93. hK32 := GetModuleHandle('kernel32');
  94. profileDir := GetEnvironmentVariable('USERPROFILE');
  95. if GetProcAddress(hK32, 'GetLocaleInfoEx') <> nil then begin
  96. // Vista
  97. KillDir(GetEnvironmentVariable('APPDATA')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  98. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  99. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  100. profileDir := profileDir+'\AppData\Roaming\DMDirc';
  101. end
  102. else begin
  103. // Not Vista
  104. KillDir(GetEnvironmentVariable('USERPROFILE')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  105. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Application Data\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  106. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  107. profileDir := profileDir+'\Application Data\DMDirc';
  108. end;
  109. // Remove irc:// handler if it is us.
  110. deleteProtocol := false;
  111. Reg := TRegistry.Create;
  112. Reg.RootKey := HKEY_CLASSES_ROOT;
  113. if Reg.OpenKey('irc\Shell\open\command', false) then begin
  114. handlerInfo := Reg.ReadString('');
  115. if (handlerInfo = '"'+InstallDir+'DMDirc.exe" -c %1') then begin
  116. deleteProtocol := true;
  117. end
  118. end;
  119. Reg.CloseKey;
  120. Reg.Free;
  121. if deleteProtocol then begin
  122. Reg := TRegistry.Create;
  123. Reg.RootKey := HKEY_CLASSES_ROOT;
  124. Reg.DeleteKey('irc\Shell\open\command');
  125. Reg.DeleteKey('irc\Shell\open');
  126. Reg.DeleteKey('irc\Shell');
  127. Reg.DeleteKey('irc\DefaultIcon');
  128. Reg.DeleteKey('irc');
  129. Reg.CloseKey;
  130. Reg.Free;
  131. end;
  132. Reg := TRegistry.Create;
  133. Reg.RootKey := HKEY_LOCAL_MACHINE;
  134. Reg.DeleteKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DMDirc');
  135. Reg.CloseKey;
  136. Reg.Free;
  137. if (FileExists(profileDir+'\dmdirc.config')) then begin
  138. if MessageBox(0, PChar('A dmdirc profile has been detected ('+profileDir+')'+#13#10+' Do you want to delete it aswell?'), 'DMDirc Uninstaller', MB_YESNO) = IDYES then begin
  139. KillDir(profileDir);
  140. end;
  141. end;
  142. MessageBox(0, PChar('DMDirc has been uninstalled from "'+InstallDir+'".'), 'DMDirc Uninstaller', MB_OK);
  143. end
  144. else begin
  145. if (ExecAndWait('java -jar ' + ExtractFileDir(paramstr(0)) + '\DMDirc.jar -k', true) <> 0) then begin
  146. TempDir := GetTempDirectory;
  147. CopyFile(pchar(paramstr(0)), pchar(TempDir+'/uninstall.exe'), false);
  148. Launch(TempDir+'/uninstall.exe '+ExtractFileDir(paramstr(0))+'\');
  149. end else begin
  150. MessageBox(0, PChar('Uninstall Aborted - DMDirc is still running.'+#13#10+'Please close DMDirc before continuing'), 'DMDirc Uninstaller', MB_OK + MB_ICONEXCLAMATION);
  151. end;
  152. end;
  153. end;
  154. end.