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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. program Uninstaller;
  2. {$IFDEF FPC}
  3. {$MODE Delphi}
  4. {$ENDIF}
  5. // Use this instead of {$APPTYPE XXX}
  6. // APP_XXX is the same as {$APPTYPE XXX}
  7. // Defaults to console
  8. // This is a work-around for a bug in FPC Cross Compiling to windows in delphi
  9. // mode (IsConsole is always true)
  10. {$DEFINE APP_GUI}
  11. // This block actually does the work for the above work-around
  12. {$IFDEF APP_GUI}
  13. {$APPTYPE GUI}
  14. {$ELSE}
  15. {$IFDEF APP_FS}
  16. {$APPTYPE FS}
  17. {$ELSE}
  18. {$IFDEF APP_TOOL}
  19. {$DEFINE APP_CONSOLE}
  20. {$APPTYPE TOOL}
  21. {$ELSE}
  22. {$DEFINE APP_CONSOLE}
  23. {$APPTYPE CONSOLE}
  24. {$ENDIF}
  25. {$ENDIF}
  26. {$ENDIF}
  27. uses Windows, SysUtils, registry, Vista;
  28. {$R uninstall.res}
  29. procedure InitCommonControls; stdcall; External 'comctl32.dll' name 'InitCommonControls';
  30. procedure dowriteln(line: String);
  31. begin
  32. if IsConsole then writeln(line);
  33. end;
  34. procedure dowrite(line: String);
  35. begin
  36. if IsConsole then write(line);
  37. end;
  38. function askQuestion(Question: String): boolean;
  39. begin
  40. Result := TaskDialog(0, 'DMDirc Uninstaller', 'Question', Question, TD_ICON_QUESTION, TD_BUTTON_YES + TD_BUTTON_NO) = mrYes;
  41. end;
  42. procedure showError(context: String; ErrorMessage: String; addFooter: boolean = true);
  43. begin
  44. if addFooter then begin
  45. ErrorMessage := ErrorMessage+#13#10;
  46. ErrorMessage := ErrorMessage+#13#10+' If you feel this is incorrect, or you require some further assistance, ';
  47. ErrorMessage := ErrorMessage+#13#10+'please feel free to contact us.';
  48. end;
  49. TaskDialog(0, 'DMDirc Setup', context, ErrorMessage, TD_ICON_ERROR, TD_BUTTON_OK, true);
  50. end;
  51. procedure showmessage(message: String; context:String = 'Information');
  52. begin
  53. TaskDialog(0, 'DMDirc Uninstaller', context, message, TD_ICON_INFORMATION, TD_BUTTON_OK);
  54. end;
  55. function GetTempDirectory(): String;
  56. var
  57. buf: array[0..MAX_PATH] of Char;
  58. wintemp, temp: String;
  59. begin
  60. GetTempPath(SizeOf(buf)-1, buf);
  61. wintemp := StrPas(buf);
  62. Randomize;
  63. temp := '\DMDirc-uninstaller-'+inttostr(1000 + Random(1000));
  64. while (DirectoryExists(wintemp+temp+'\')) do begin
  65. temp := temp+'-'+inttostr(1+Random(1000));
  66. end;
  67. MkDir(wintemp+temp+'\');
  68. result := wintemp+temp+'\';
  69. end;
  70. // Run an application and don't wait for it to finish.
  71. function Launch(sProgramToRun: String; hide: boolean = false): TProcessInformation;
  72. var
  73. StartupInfo: TStartupInfo;
  74. begin
  75. FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  76. with StartupInfo do begin
  77. cb := SizeOf(TStartupInfo);
  78. dwFlags := STARTF_USESHOWWINDOW;
  79. if hide then wShowWindow := SW_HIDE
  80. else wShowWindow := SW_SHOWNORMAL;
  81. end;
  82. CreateProcess(nil, PChar(sProgramToRun), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, Result);
  83. end;
  84. // Run an application and wait for it to finish.
  85. function ExecAndWait(sProgramToRun: String; hide: boolean = false): Longword;
  86. var
  87. ProcessInfo: TProcessInformation;
  88. begin
  89. ProcessInfo := Launch(sProgramToRun, hide);
  90. getExitCodeProcess(ProcessInfo.hProcess, Result);
  91. while Result=STILL_ACTIVE do begin
  92. sleep(1000);
  93. GetExitCodeProcess(ProcessInfo.hProcess, Result);
  94. end;
  95. end;
  96. function KillDir(Dir: string): Integer;
  97. var
  98. searchResult: TSearchRec;
  99. begin
  100. Result := 0;
  101. if FindFirst(Dir+'\*', faDirectory + faHidden + faReadOnly + faSysfile + faAnyFile, searchResult) = 0 then
  102. begin
  103. repeat
  104. if (searchResult.attr and faDirectory) <> faDirectory then begin
  105. Try
  106. DeleteFile(Dir+'\'+searchResult.name);
  107. Except
  108. MessageBox(0, PChar('Unable to delete "'+Dir+'\'+searchResult.name+'" - is DMDirc still running?.'), 'DMDirc Uninstaller', MB_OK);
  109. end;
  110. end
  111. else begin
  112. if (searchResult.name <> '.') and (searchResult.name <> '..') then begin
  113. KillDir(Dir+'\'+searchResult.name);
  114. end;
  115. end;
  116. until FindNext(searchResult) <> 0;
  117. FindClose(searchResult);
  118. end;
  119. Try
  120. RmDir(Dir);
  121. Except
  122. end;
  123. end;
  124. var
  125. TempDir: String;
  126. InstallDir: String = '';
  127. i: Integer;
  128. Reg: TRegistry;
  129. handlerInfo: String;
  130. profileDir: String;
  131. deleteProtocol: boolean;
  132. begin
  133. InitCommonControls;
  134. if (ParamCount > 0) then begin
  135. for i := 1 to ParamCount do begin
  136. InstallDir := InstallDir+' '+paramstr(i);
  137. end;
  138. InstallDir := trim(InstallDir);
  139. KillDir(InstallDir);
  140. profileDir := GetEnvironmentVariable('USERPROFILE');
  141. if IsWindowsVista then begin
  142. // Vista
  143. KillDir(GetEnvironmentVariable('APPDATA')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  144. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  145. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  146. profileDir := profileDir+'\AppData\Roaming\DMDirc';
  147. end
  148. else begin
  149. // Not Vista
  150. KillDir(GetEnvironmentVariable('USERPROFILE')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  151. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Application Data\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  152. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  153. profileDir := profileDir+'\Application Data\DMDirc';
  154. end;
  155. // Remove irc:// handler if it is us.
  156. deleteProtocol := false;
  157. Reg := TRegistry.Create;
  158. Reg.RootKey := HKEY_CLASSES_ROOT;
  159. if Reg.OpenKey('irc\Shell\open\command', false) then begin
  160. handlerInfo := Reg.ReadString('');
  161. if (handlerInfo = '"'+InstallDir+'DMDirc.exe" -c %1') then begin
  162. deleteProtocol := true;
  163. end
  164. end;
  165. Reg.CloseKey;
  166. Reg.Free;
  167. if deleteProtocol then begin
  168. Reg := TRegistry.Create;
  169. Reg.RootKey := HKEY_CLASSES_ROOT;
  170. Reg.DeleteKey('irc\Shell\open\command');
  171. Reg.DeleteKey('irc\Shell\open');
  172. Reg.DeleteKey('irc\Shell');
  173. Reg.DeleteKey('irc\DefaultIcon');
  174. Reg.DeleteKey('irc');
  175. Reg.CloseKey;
  176. Reg.Free;
  177. end;
  178. Reg := TRegistry.Create;
  179. Reg.RootKey := HKEY_LOCAL_MACHINE;
  180. Reg.DeleteKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DMDirc');
  181. Reg.CloseKey;
  182. Reg.Free;
  183. if (FileExists(profileDir+'\dmdirc.config')) then begin
  184. if MessageBox(0, PChar('A dmdirc profile has been detected ('+profileDir+') '+#13#10+'Do you want to delete it aswell?'), 'DMDirc Uninstaller', MB_YESNO) =
  185. IDYES then begin
  186. KillDir(profileDir);
  187. end;
  188. end;
  189. showmessage('DMDirc has been uninstalled from "'+InstallDir+'".', 'Uninstall Successful');
  190. end
  191. else if askQuestion('This will uninstall DMDirc. '+#13#10+#13#10+'Do you want to continue?') then begin
  192. if (ExecAndWait('java -jar "' + ExtractFileDir(paramstr(0)) + '\DMDirc.jar" -k', true) <> 0) then begin
  193. TempDir := GetTempDirectory;
  194. CopyFile(pchar(paramstr(0)), pchar(TempDir+'/uninstall.exe'), false);
  195. Launch('"'+TempDir+'/uninstall.exe" '+ExtractFileDir(paramstr(0))+'\');
  196. end else begin
  197. showError('Uninstall Aborted - DMDirc is still running.', 'Please close DMDirc before continuing')
  198. end;
  199. end;
  200. end.