It's obvious. cmd.exe may try to delete the exe before the program is completely ended. In that case it will fail.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.
Can an exe be made to delete itself?
Re: Can an exe be made to delete itself?
Re: Can an exe be made to delete itself?
> 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.
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.
"PureBasic won't be object oriented, period" - Fred.
Re: Can an exe be made to delete itself?
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?
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.
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?
> 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.
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.
"PureBasic won't be object oriented, period" - Fred.
Re: Can an exe be made to delete itself?
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.
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?
This works on Windows 7 as admin...
But, I still see the cmd.exe window with or without #PB_Program_Hide?
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
Re: Can an exe be made to delete itself?
12 years later, I can report that it works silently on Windows 2022.
Re: Can an exe be made to delete itself?
If I remember well, Windows as a way to delete a file on the next reboot.
Anyone have the code snippet?
Anyone have the code snippet?
Re: Can an exe be made to delete itself?
Add the uninstaller to one of the RunOnce sections, and after execution the run line will be removed from the registryCaronte3D 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?
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?
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).
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).
Re: Can an exe be made to delete itself?
Doesn't work for me, if call GetLastError_() after it the return code is 5 (ERROR_ACCESS_DENIED) 
Maybe it need administrator rights

Maybe it need administrator rights
Last edited by Caronte3D on Mon Sep 02, 2024 5:20 pm, edited 1 time in total.
Re: Can an exe be made to delete itself?
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))
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?
You are right.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
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).
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).