Can an exe be made to delete itself?

Just starting out? Need help? Post your questions and find answers here.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Can an exe be made to delete itself?

Post by Trond »

netmaestro wrote:Trond, could you elaborate please? How does timing luck affect it? Since PB posted that quite some time ago I've used it and never saw it fail.
It's obvious. cmd.exe may try to delete the exe before the program is completely ended. In that case it will fail.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Can an exe be made to delete itself?

Post by PB »

> cmd.exe may try to delete the exe before the program is completely ended

Okay, here's my "fixed" version that should be Trond-Approved(tm).
You can confirm it works by watching the Task Manager for the exe.
Our exe disappears instantly when our app ends after we dismiss the
MessageRequester, and then cmd.exe appears for one second before
it too disappears after doing the deletion.

Code: Select all

MessageBox_(0,"This exe will self-destruct 1 second after you click OK","Fixed",0)
a$="ping -n 2 127.0.0.1 >NUL"+#CRLF$+"del "+Chr(34)+GetFilePart(ProgramFilename())+Chr(34)+#CRLF$+"del temp.bat"
If CreateFile(0,"temp.bat") : WriteStringN(0,a$) : CloseFile(0) : EndIf
ShellExecute_(0,"open","temp.bat","","",#SW_HIDE)
End
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Can an exe be made to delete itself?

Post by Trond »

It will probably work in 100% of cases, however, this doesn't mean it's guaranteed to work. It's still not guaranteed to work, because the exe may still take a longer time to quit than the delay (remember there is PB end code to free gadgets, images and memory, close files, etc... that we don't see).
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: Can an exe be made to delete itself?

Post by TomS »

Check this out: http://purearea.net/pb/CodeArchiv/Windo ... OwnExe2.pb
The Exe is deleted after a reboot.
I have no idea if this actually works on Vista/Win7. On XP I think it worked.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Can an exe be made to delete itself?

Post by PB »

> the exe may still take a longer time to quit than the delay

So increase the delay from 1 second to 10. :)

> remember there is PB end code to free gadgets, images and
> memory, close files, etc... that we don't see

I know, but seriously: how long does all that take? Assuming
that the app has done its shutdown procedures (eg. deletes
all its temp files, frees images, etc) properly -- leaving only
an "End" statement for PureBasic to do.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Can an exe be made to delete itself?

Post by Thorium »

Many years ago i did it with VB6 with another methode. Dont know if this is working on XP/Vista/7.
Copy the .exe to Recycle.Bin folder and run it there, pass the path of the .exe to delete as command line parameter.
So the .exe in Recycle.Bin will delete the original .exe.
The next time recycle bin gets cleared the copied .exe is gone.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Can an exe be made to delete itself?

Post by skywalk »

This works on Windows 7 as admin...
But, I still see the cmd.exe window with or without #PB_Program_Hide?

Code: Select all

; COMPILER OPTIONS:
;   [ ] Enable inline ASM support
;   [ ] Create unicode executable
;   [ ] Create threadsafe executable
;   [ ] Enable OnError lines support
;   [ ] Enable XP skin support
;   [x] Request Administrator mode for Windows Vista
;   [x] Request User mode for Windows Vista (no virtualization)
;   Library Subsystem:  
;   Executable Format:  Windows   ;|Console|Shared DLL
;   CPU:                All       ;|Dynamic|w/MMX|w/3DNOW|w/SSE|w/SSE2
;   File Format:        UTF-8
;   Compiler:           PureBasic 5.00 Beta 6 (x86)
Define.s r$ = "I will delete myself in 2 seconds using cmd.exe..."
MessageRequester("Self Deleting Exe Example", r$, #MB_ICONINFORMATION)
RunProgram(GetEnvironmentVariable("comspec"),"/c " + "timeout 2" + " && " + "del " + #DQUOTE$ + ProgramFilename() + #DQUOTE$, "", #PB_Program_Hide)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
elwood
User
User
Posts: 30
Joined: Tue Dec 27, 2016 4:40 pm
Location: Lyon, France

Re: Can an exe be made to delete itself?

Post by elwood »

12 years later, I can report that it works silently on Windows 2022.
AmigaOS betatester
French Amiga Translation Team
Team for the Planet member
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Can an exe be made to delete itself?

Post by Caronte3D »

If I remember well, Windows as a way to delete a file on the next reboot.
Anyone have the code snippet?
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Can an exe be made to delete itself?

Post by AZJIO »

Caronte3D wrote: Mon Sep 02, 2024 10:32 am If I remember well, Windows as a way to delete a file on the next reboot.
Anyone have the code snippet?
Add the uninstaller to one of the RunOnce sections, and after execution the run line will be removed from the registry
KEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Can an exe be made to delete itself?

Post by Axolotl »

Maybe this works (windows only)

Code: Select all

MoveFileEx_("Filename_to_Delete.exe", 0, #MOVEFILE_DELAY_UNTIL_REBOOT) 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Can an exe be made to delete itself?

Post by Caronte3D »

Doesn't work for me, if call GetLastError_() after it the return code is 5 (ERROR_ACCESS_DENIED) :?
Maybe it need administrator rights
Last edited by Caronte3D on Mon Sep 02, 2024 5:20 pm, edited 1 time in total.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Can an exe be made to delete itself?

Post by Marc56us »

Use the installer/uninstaller system
1. The program copies itself into the Temporary directory
2. It launches the copy in temp and terminates (use RunProgram but not with #PB_Program_Wait!).
3. Once the copy has been launched in Temp, it deletes the original (it knows it's a copy because it's in the Temp directory).
4. Use a RunOnce registry key to delete the temporary file on reboot (or au batch in startup folder (Win+R shell:startup to know where it is))
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Can an exe be made to delete itself?

Post by Axolotl »

Caronte3D wrote: Mon Sep 02, 2024 5:01 pm Doesn't work for me, if call GetLastError_() after it the return code is 5 (ERROR_ACCESS_DENIED)
Maybe it need administrator rights
You are right.
This value can be used only if the process is in the context of a user who belongs to the administrators group or the LocalSystem account.
The function uses a registry key HKLM/.....
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply