Mega Code Archive

 
Categories / Delphi / System
 

Program Self Removal (Utilizing Windows Registry)

Title: Program Self Removal (Utilizing Windows Registry) Question: How do you make a program that can remove itself? Answer: This simple method uses a Windows Registry entry, which in turn, makes Command.com to do the job for us, whenever the next Windows restart occurs. Steps Taken: 1. Add Registry to uses 2. Add the following code to a procedure of your choice procedure TForm1.Button1Click(Sender: TObject); var APath: array[0..255] of char; begin {Command.com does not support long paths, so convert to short} if GetShortPathName(PChar(ParamStr(0)),APath,SizeOf(APath) - 1) 0 then begin {Work with TRegistry} with TRegistry.Create do try {Set Root Key} RootKey := HKEY_LOCAL_MACHINE; {Open Key, creating key if it does not exist} if OpenKey('\Software\Microsoft\Windows\CurrentVersion\RunOnce',True) then begin {Add our String Value to the Key} WriteString('MyApp','command.com /c del ' + APath); {Close the Key} CloseKey; end; finally {Free TRegistry} Free; end; end; end; Code Note: This example makes use of ParamStr(0) to meet the expectations that the title of this article has elicited: By pointing to the path+filename of the application that executes this procedure, the program will in fact bring about the removal of itself. Windows NT/2000 Note: Users running programs that utilize this code, must have the right to modify the HKEY_LOCAL_MACHINE section of the Windows Registry.