Page 2 of 2

Re: Can an exe be made to delete itself?

Posted: Mon Oct 25, 2010 7:26 pm
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.

Re: Can an exe be made to delete itself?

Posted: Tue Oct 26, 2010 12:52 am
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

Re: Can an exe be made to delete itself?

Posted: Tue Oct 26, 2010 10:22 am
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).

Re: Can an exe be made to delete itself?

Posted: Tue Oct 26, 2010 10:37 am
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.

Re: Can an exe be made to delete itself?

Posted: Tue Oct 26, 2010 10:51 am
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.

Re: Can an exe be made to delete itself?

Posted: Tue Oct 26, 2010 11:58 am
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.

Re: Can an exe be made to delete itself?

Posted: Sat Oct 20, 2012 8:24 pm
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)

Re: Can an exe be made to delete itself?

Posted: Sun Sep 01, 2024 10:54 pm
by elwood
12 years later, I can report that it works silently on Windows 2022.

Re: Can an exe be made to delete itself?

Posted: Mon Sep 02, 2024 10:32 am
by Caronte3D
If I remember well, Windows as a way to delete a file on the next reboot.
Anyone have the code snippet?

Re: Can an exe be made to delete itself?

Posted: Mon Sep 02, 2024 10:52 am
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

Re: Can an exe be made to delete itself?

Posted: Mon Sep 02, 2024 10:58 am
by Axolotl
Maybe this works (windows only)

Code: Select all

MoveFileEx_("Filename_to_Delete.exe", 0, #MOVEFILE_DELAY_UNTIL_REBOOT) 

Re: Can an exe be made to delete itself?

Posted: Mon Sep 02, 2024 5:01 pm
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

Re: Can an exe be made to delete itself?

Posted: Mon Sep 02, 2024 5:15 pm
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))

Re: Can an exe be made to delete itself?

Posted: Mon Sep 02, 2024 10:08 pm
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/.....