Self Deleting Exe after Running (new version)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Self Deleting Exe after Running (new version)

Post by Rings »

Broken on newer Windows. Alternatives below.

below is a sourcecode to write an uninstaller which delete itself after running (goto deleteItself ) , no restart or Batchfile needed .
this version used the possibility to run code from stack :)

Code: Select all

; Name: Self deleter
; Purpose:Good to use for an Uninstaller
;
; This file (if compiled to exe) delete itself after finishing (executes code on the stack ;) )
; should also work under Win89, but never tested
;
;Enable InlineASM !

;
; coded/adapted/enhanced  (Powerbasic-version from Wayne Diamond, thx wayne :) )
; by Siegfried Rings
; have fun !
MessageRequester("info","self-deleter",0)


DeleteItself:
 hModule = GetModuleHandle_(0)
 szModuleName.s=Space(512)
 GetModuleFileName_(hModule, szModuleName, Len(szModuleName))
 hKrnl32 = GetModuleHandle_("kernel32.dll")
 pExitProcess = GetProcAddress_(hKrnl32, "ExitProcess")
 pDeleteFile = GetProcAddress_(hKrnl32, "DeleteFileA")
 pFreeLibrary = GetProcAddress_(hKrnl32, "FreeLibrary")
 pUnmapViewOfFile = GetProcAddress_(hKrnl32, "UnmapViewOfFile")


;Set Fileattributes if Writeprotectet!
SetFileAttributes_(szModuleName,0)

#Win9x = 0  ;do OS-detection code by yourself !
If #Win9x <> 0 
; MessageRequester("Info"," Win95/98/ME ",0)
 MOV eax,szModuleName 
 PUSH 0
 PUSH 0
 PUSH eax
 PUSH pExitProcess
 PUSH hModule
 PUSH pDeleteFile
 PUSH pFreeLibrary
 RET
Else
; MessageRequester("Info"," NT4/2K/XP/.NET ",0)
 CloseHandle_(4)
 MOV eax,szModuleName 
 PUSH 0
 PUSH 0
 PUSH eax
 PUSH pExitProcess
 PUSH hModule
 PUSH pDeleteFile
 PUSH pUnmapViewOfFile
 RET
EndIf 
SPAMINATOR NR.1
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

tested succesfully under NT4 and W2000. Can anyone confirm this also under alternative Windows-OS's please ?
SPAMINATOR NR.1
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

does not work on my win XP pro danish.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Self Deleting Exe after Running (new version)

Post by PB »

Here's my version that needs no restart, batch file, inline ASM, or OS check:

Code: Select all

; Delete running exe, by PB.  Works under all versions of Windows.

MessageBox_(0,"This exe will self-destruct when you click OK","Test",0)
RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)
[Edit] Updated for v4.02 on June 4, 2007 by myself. Much shorter now!
Last edited by PB on Mon Jun 04, 2007 10:50 am, edited 3 times in total.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

@pb: that works well
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Self Deleting Exe after Running (new version)

Post by NoahPhense »

Very nice PB.. letting my old version (the batch file one) delete itself,
for good.. ;)

- np
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

that's a cool trick Rings :)
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

Rings wrote:tested succesfully under NT4 and W2000. Can anyone confirm this also under alternative Windows-OS's please ?
Sadly, doesn't on Win XP german.
Randy Walker
Addict
Addict
Posts: 989
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

Less sophisticated, but 100 percent compatible with any version of Windows. Just let your main app clean up for you...

Code: Select all

;For our purposes, consider "target.exe" to be your main
;application.  "Transit.exe" is the source file used to
;replace your main application. The following code would
;be placed near the top of your main code or wherever you
;think most convenient. By placing it inside your main
;application, you force the installation update even if
;the user is unaware that an update is available.  You
;can insert code into your main application to allow the
;user to bypass the update if desirable.

; SKIP THIS BLOCK IF NO UPDATE "FILE" AVAILABLE
If FileSize("C:\mypath\install.exe") > 0
  ;
  ; ON THE 1ST PASS ** LAUNCH UPDATE & QUIT **
  If FileSize("C:\mypath\transit.exe") > 0 ;(our new target.exe)
    RunProgram("C:\mypath\install.exe","","")
    End ; QUIT!!! target.exe to allow installation update!!!
    ;
    ; Meanwhile install.exe procedes as follows:
    ;   Delete target.exe  (your old app)
    ;   Rename transit.exe as target.exe
    ;   LAUNCH THE NEW TARGET.EXE
    ;   exit install.exe
    ; 
  Else
    ; ON THE 2ND PASS ** TIDY UP & RESUME **
    DeleteFile("C:\mypath\install.exe")
    ;transit.exe didn't exist so remove the installer.
    ;The update is either complete or transit.exe was not
    ;available to replace the old target.exe file.
  EndIf
EndIf
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Self Deleting Exe after Running (new version)

Post by Joakim Christiansen »

PB wrote:Here's my version that needs no restart, batch file, inline ASM, or OS check...
Very nice, but what do I write to delete the whole directory it's in?

Anyone? Hehe, it's the latest line of code I need to finish my installer/uninstaller thingy...
I like logic, hence I dislike humans but love computers.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

hm...

what about letting the deleter start from the temp-dir,
deleting the app-path, and then deleting itself like PB suggested?
oh... and have a nice day.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I could try that, but I wonder why this is not working...

Code: Select all

rmdir /s /q c:\path
I like logic, hence I dislike humans but love computers.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Code: Select all

If ProgramParameter(0)
  DeleteDirectory(Left(ProgramParameter(0),Len(ProgramParameter(0))-1),"*.*",#PB_FileSystem_Recursive|#PB_FileSystem_Force)
  comspec$=Space(255): GetEnvironmentVariable_("comspec",comspec$,255)
  RunProgram(comspec$,"/c del "+#DQUOTE$+GetPathPart(ProgramFilename())+#DQUOTE$,"")
Else
  If MessageRequester("Uninstall","Are you shure you want to completely remove this folder and all the files in it?",#MB_ICONQUESTION|#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
    CopyFile(ProgramFilename(),GetTemporaryDirectory()+"TVUninstall.exe")
    RunProgram(GetTemporaryDirectory()+"TVUninstall.exe",#DQUOTE$+GetPathPart(ProgramFilename())+#DQUOTE$,"")
  EndIf
EndIf
Tried running it from the temp-directory like this and then delete the directory I gave it in a parameter, but it didn't work.
I like logic, hence I dislike humans but love computers.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

o_O

this code is so readable... it's an unbreakable argument for multi-line code...

I have to screw it apart to look through...
oh... and have a nice day.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Kaeru Gaman wrote:o_O

this code is so readable... it's an unbreakable argument for multi-line code...
Hehe, yeah I know when I posted it here it looks like crap...
I like logic, hence I dislike humans but love computers.
Post Reply