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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. program Uninstaller;
  2. {$MODE Delphi}
  3. {$APPTYPE GUI}
  4. uses Windows, SysUtils, classes, registry, Vista;
  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. i: Integer;
  79. Reg: TRegistry;
  80. handlerInfo: String;
  81. profileDir: String;
  82. deleteProtocol: boolean;
  83. begin
  84. if (ParamCount > 0) then begin
  85. for i := 1 to ParamCount do begin
  86. InstallDir := InstallDir+' '+paramstr(i);
  87. end;
  88. InstallDir := trim(InstallDir);
  89. KillDir(InstallDir);
  90. profileDir := GetEnvironmentVariable('USERPROFILE');
  91. if IsWindowsVista then begin
  92. // Vista
  93. KillDir(GetEnvironmentVariable('APPDATA')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  94. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  95. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  96. profileDir := profileDir+'\AppData\Roaming\DMDirc';
  97. end
  98. else begin
  99. // Not Vista
  100. KillDir(GetEnvironmentVariable('USERPROFILE')+'\Microsoft\Windows\Start Menu\Programs\DMDirc');
  101. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Application Data\Microsoft\Internet Explorer\Quick Launch\DMDirc.lnk');
  102. DeleteFile(GetEnvironmentVariable('USERPROFILE')+'\Desktop\DMDirc.lnk');
  103. profileDir := profileDir+'\Application Data\DMDirc';
  104. end;
  105. // Remove irc:// handler if it is us.
  106. deleteProtocol := false;
  107. Reg := TRegistry.Create;
  108. Reg.RootKey := HKEY_CLASSES_ROOT;
  109. if Reg.OpenKey('irc\Shell\open\command', false) then begin
  110. handlerInfo := Reg.ReadString('');
  111. if (handlerInfo = '"'+InstallDir+'DMDirc.exe" -c %1') then begin
  112. deleteProtocol := true;
  113. end
  114. end;
  115. Reg.CloseKey;
  116. Reg.Free;
  117. if deleteProtocol then begin
  118. Reg := TRegistry.Create;
  119. Reg.RootKey := HKEY_CLASSES_ROOT;
  120. Reg.DeleteKey('irc\Shell\open\command');
  121. Reg.DeleteKey('irc\Shell\open');
  122. Reg.DeleteKey('irc\Shell');
  123. Reg.DeleteKey('irc\DefaultIcon');
  124. Reg.DeleteKey('irc');
  125. Reg.CloseKey;
  126. Reg.Free;
  127. end;
  128. Reg := TRegistry.Create;
  129. Reg.RootKey := HKEY_LOCAL_MACHINE;
  130. Reg.DeleteKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DMDirc');
  131. Reg.CloseKey;
  132. Reg.Free;
  133. if (FileExists(profileDir+'\dmdirc.config')) then begin
  134. 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
  135. KillDir(profileDir);
  136. end;
  137. end;
  138. MessageBox(0, PChar('DMDirc has been uninstalled from "'+InstallDir+'".'), 'DMDirc Uninstaller', MB_OK);
  139. end
  140. 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
  141. if (ExecAndWait('java -jar ' + ExtractFileDir(paramstr(0)) + '\DMDirc.jar -k', true) <> 0) then begin
  142. TempDir := GetTempDirectory;
  143. CopyFile(pchar(paramstr(0)), pchar(TempDir+'/uninstall.exe'), false);
  144. Launch(TempDir+'/uninstall.exe '+ExtractFileDir(paramstr(0))+'\');
  145. end else begin
  146. MessageBox(0, PChar('Uninstall Aborted - DMDirc is still running.'+#13#10+'Please close DMDirc before continuing'), 'DMDirc Uninstaller', MB_OK + MB_ICONEXCLAMATION);
  147. end;
  148. end;
  149. end.