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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. end;
  47. function KillDir(Dir: string): Integer;
  48. var
  49. searchResult: TSearchRec;
  50. begin
  51. Result := 0;
  52. if FindFirst(Dir+'\*', faDirectory + faHidden + faReadOnly + faSysfile + faAnyFile, searchResult) = 0 then
  53. begin
  54. repeat
  55. if (searchResult.attr and faDirectory) <> faDirectory then begin
  56. Try
  57. DeleteFile(Dir+'\'+searchResult.name);
  58. Except
  59. MessageBox(0, PChar('Unable to delete "'+Dir+'\'+searchResult.name+'" - is DMDirc still running?.'), 'DMDirc Uninstaller', MB_OK);
  60. end;
  61. end
  62. else begin
  63. if (searchResult.name <> '.') and (searchResult.name <> '..') then begin
  64. KillDir(Dir+'\'+searchResult.name);
  65. end;
  66. end;
  67. until FindNext(searchResult) <> 0;
  68. FindClose(searchResult);
  69. end;
  70. Try
  71. RmDir(Dir);
  72. Except
  73. end;
  74. end;
  75. var
  76. TempDir: String;
  77. InstallDir: String = '';
  78. hK32: THandle;
  79. i: Integer;
  80. Reg: TRegistry;
  81. handlerInfo: String;
  82. profileDir: String;
  83. deleteProtocol: boolean;
  84. begin
  85. if (ParamCount > 0) then begin
  86. for i := 1 to ParamCount do begin
  87. InstallDir := InstallDir+' '+paramstr(i);
  88. end;
  89. InstallDir := trim(InstallDir);
  90. KillDir(InstallDir);
  91. hK32 := GetModuleHandle('kernel32');
  92. profileDir := GetEnvironmentVariable('USERPROFILE');
  93. if GetProcAddress(hK32, 'GetLocaleInfoEx') <> nil then begin
  94. // Vista
  95. KillDir(GetEnvironmentVariable('APPDATA')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  96. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  97. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  98. profileDir := profileDir+'\AppData\Roaming\DMDirc';
  99. end
  100. else begin
  101. // Not Vista
  102. KillDir(GetEnvironmentVariable('USERPROFILE')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  103. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Application Data\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  104. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  105. profileDir := profileDir+'\Application Data\DMDirc';
  106. end;
  107. // Remove irc:// handler if it is us.
  108. deleteProtocol := false;
  109. Reg := TRegistry.Create;
  110. Reg.RootKey := HKEY_CLASSES_ROOT;
  111. if Reg.OpenKey('irc\Shell\open\command', false) then begin
  112. handlerInfo := Reg.ReadString('');
  113. if (handlerInfo = '"'+InstallDir+'DMDirc.exe" -c %1') then begin
  114. deleteProtocol := true;
  115. end
  116. end;
  117. Reg.CloseKey;
  118. Reg.Free;
  119. if deleteProtocol then begin
  120. Reg := TRegistry.Create;
  121. Reg.RootKey := HKEY_CLASSES_ROOT;
  122. Reg.DeleteKey('irc\Shell\open\command');
  123. Reg.DeleteKey('irc\Shell\open');
  124. Reg.DeleteKey('irc\Shell');
  125. Reg.DeleteKey('irc\DefaultIcon');
  126. Reg.DeleteKey('irc');
  127. Reg.CloseKey;
  128. Reg.Free;
  129. end;
  130. Reg := TRegistry.Create;
  131. Reg.RootKey := HKEY_LOCAL_MACHINE;
  132. Reg.DeleteKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DMDirc');
  133. Reg.CloseKey;
  134. Reg.Free;
  135. if (FileExists(profileDir+'\dmdirc.config')) then begin
  136. 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
  137. KillDir(profileDir);
  138. end;
  139. end;
  140. MessageBox(0, PChar('DMDirc has been uninstalled from "'+InstallDir+'".'), 'DMDirc Uninstaller', MB_OK);
  141. end
  142. else if MessageBox(0, PChar('This will uninstall DMDirc.'+#13#10+#13#10+'Do you want to continue?'), 'DMDirc Uninstaller', MB_YESNO) = IDYES then begin
  143. if (ExecAndWait('java -jar ' + ExtractFileDir(paramstr(0)) + '\DMDirc.jar -k', true) <> 0) then begin
  144. TempDir := GetTempDirectory;
  145. CopyFile(pchar(paramstr(0)), pchar(TempDir+'/uninstall.exe'), false);
  146. Launch(TempDir+'/uninstall.exe '+ExtractFileDir(paramstr(0))+'\');
  147. end else begin
  148. MessageBox(0, PChar('Uninstall Aborted - DMDirc is still running.'+#13#10+'Please close DMDirc before continuing'), 'DMDirc Uninstaller', MB_OK + MB_ICONEXCLAMATION);
  149. end;
  150. end;
  151. end.