Mega Code Archive

 
Categories / Delphi / Examples
 

How to delete our own application the professional way

Title: How to delete our own application: the professional way Question: How to uninstall an application leaving the system completly clean? After 4 betas, there's the way to do things professional. Answer: After reading Misha's article on how to remove the application file (http://www.delphi3000.com/articles/article_514.asp), i wrote down my solutions (http://www.delphi3000.com/articles/article_2256.asp), but I called them as "betas" since every one of them had PRO and VS. The final solutions is now available. Taking from Beta 2, we can create a different program to put into the temp folder. After executing the deletion of the main-application file it will sign up for self delection at reboot with a simple MoveFileEx API call. Specifing the MOVEFILE_DELAY_UNTIL_REBOOT flag into the dwFlags parameter, the MoveFileEx will plan the operation to be runned at next reboot. Actually, MoveFileEx is used to rename or move a file, but you can also use NIL as the destination parameter and that will mean "delete the file". Example: MoveFileEx('C:\temp\FileToRemove.dat', nil, MOVEFILE_REPLACE_EXISTING or MOVEFILE_DELAY_UNTIL_REBOOT); I also bettered it to not loop more that 5 seconds. You can change this changing the MAX_RUN_TIME constant value (in milliseconds). The DelFile source code will then be as follow: program DelFile; {$APPTYPE CONSOLE} uses SysUtils, Windows; const MAX_RUN_TIME=5000; var StartTime: dword; begin if ParamCount=0 then Exit; // no file specified for deletion StartTime:=GetTickCount(); repeat sleep(10); until DeleteFile(PChar(ParamStr(1))) or (GetTickCount()-StartTimeMAX_RUN_TIME); MoveFileEx(PChar(ParamStr(0)), nil, MOVEFILE_REPLACE_EXISTING or MOVEFILE_DELAY_UNTIL_REBOOT); end. That's is, but unfortunately it seems to work only in NT-based systems.