Win32 - Delete running application (uninstall)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi @ all!
Finally i found a way how to delete your running app automatical, like some uninstallers do the job. I need this routine for an own install/uninstaller system and think maybe someone is interested to learn about the following (maybe danger) code...

Maybe there is any other and better methode... But i dont know and for the first time it seems to works fine...

Code: Select all

; ------------------------------------------------------------
;
; PureBasic Win32 API - MyDeleteRunningApp - Example v1.0
;
; by MrVainSCL! aka Thorsten   19/Jan/2003    PB v3.51+
;
; ------------------------------------------------------------
;
    Procedure MyDeleteRunningApp()
        ;
        ; -------- Get application filename --------
        ;
        a$ = Space(1024) 
        GetModuleFilename_(0,@a$,1024)            
        appname$ = GetFilePart(a$)                
        ;
        ; -------- Create script file (uninstall) --------     
        ;
        OpenFile(0,"tmp.bat")
          WriteStringN(":loop")
          WriteStringN("del "+appname$)   ;programm.exe")
          WriteStringN("if exist "+appname$+" Goto loop")
          WriteStringN("del tmp.bat")
        CloseFile(0)
        ;
        ; -------- Execute script and delete running app --------
        ;
        ShellExecute_(0,"open","tmp.bat",0,0,#SW_SHOW)
        ;
    EndProcedure
    ;
    ; -------- We will quit, so delete running app -------- 
    ;   
    MessageRequester("Delete running app","Will now delete your running app!",0)
    MyDeleteRunningApp()                                
End
;
; ------------------------------------------------------------
Create an executeable to any directory and test it... After starting this application a small requester appears, the program will quit and the file you have started is now deleted!


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX9.0, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Maybe there is any other and better methode...

Hi MrVain,

Your tip works well but has one problem: if the executable name has a
space in it, it fails. So change the appname$ line to this instead, and
it'll work with any executable name:

appname$=Chr(34)+GetFilePart(a$)+Chr(34)

Also, to stop the batch file from briefly popping up (which looks bad),
change the #SW_SHOW flag to #SW_HIDE instead -- much better! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Ok PB, i have noticed it - thanks! :)

greetz
MrVainSCL! aka Thorsten

PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX9.0, SB AWE64, Win2000 + all Updates...
darklordz
Enthusiast
Enthusiast
Posts: 119
Joined: Wed May 21, 2003 1:44 pm
Location: Netherlands
Contact:

Post by darklordz »

Whynot just do getshortfilename from windows api, it returnd the dos filename..../path aka 6chrs~1
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Post by Intrigued »

BackupUser wrote:<i>Restored from previous forum. Originally posted by <b>PB</b>.</i><br /><br /> > Maybe there is any other and better methode...<br /><br />Hi MrVain,<br /><br />Your tip works well but has one problem: if the executable name has a<br />space in it, it fails. So change the appname$ line to this instead, and<br />it'll work with any executable name:<br /><br />appname$=Chr(34)+GetFilePart(a$)+Chr(34)<br /><br />Also, to stop the batch file from briefly popping up (which looks bad),<br />change the #SW_SHOW flag to #SW_HIDE instead -- much better! <img src="images/smiles/icon_smile.gif" /><br />
Q.

Is there a time I should use Chr(34) over say "?
Intrigued - Registered PureBasic, lifetime updates user
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

If you use " you end the string.
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Post by Thorsten1867 »

I use following code for EasySetup:

Code: Select all

Procedure SelfDestruct(ProgDir$="") 
  exe$ = ProgramFilename()
  bat$ = Left(exe$,1)+":\~~uninst.bat"
  If ProgDir$ And Right(ProgDir$,1) = "\" : ProgDir$ = Left(ProgDir$,Len(ProgDir$)-1) : EndIf 
  If CreateFile(0, bat$) 
    WriteStringN(0, "cd \")
    WriteStringN(0, ":DeleteFile")
    WriteStringN(0, "del "+Chr(34)+exe$+Chr(34)) ;program executable
    WriteStringN(0, "if exist "+Chr(34)+exe$+Chr(34)+" goto DeleteFile")
    If ProgDir$
      WriteStringN(0, "rd "+Chr(34)+ProgDir$+Chr(34)) ; program directory
    EndIf
    WriteStringN(0, "del "+Chr(34)+bat$+Chr(34)) ; temporary batch file
    WriteStringN(0, "exit") 
    CloseFile(0)
    ShellExecute_(0,"open",bat$,0,0,#SW_HIDE)
  EndIf 
EndProcedure
Last edited by Thorsten1867 on Fri Oct 27, 2006 4:05 pm, edited 1 time in total.
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Note that the last script fails if the path to the program contains characters DOS can't handle.

Even though DOS can handle long filenames, it can not handle some filenames that are perfectly ok in Windows.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Win32 - Delete running application (uninstall)

Post by netmaestro »

I'm surprised to find this one isn't posted here, I got it from user PB some time back and it seems quite reliable:

Code: Select all

RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"", #PB_Program_Hide) 
BERESHEIT
Thorium
Addict
Addict
Posts: 1271
Joined: Sat Aug 15, 2009 6:59 pm

Re: Win32 - Delete running application (uninstall)

Post by Thorium »

Nice ways.
I have used another way. But that is a long time ago. It worked on Win95 don't know if it works on newer OS's. I think it's not the best way anyway. Just telling it for fun:

I copied the own .exe to the recycle.bin directory on c: and executed it there with a parameter to tell it to delete the original .exe and the path to it. Then terminated the original .exe. The copied .exe in recycle.bin waited for the original to terminate and deleted it. The next time the recycle bin was been cleared the copied .exe was gone.
utopiomania wrote:Note that the last script fails if the path to the program contains characters DOS can't handle.

Even though DOS can handle long filenames, it can not handle some filenames that are perfectly ok in Windows.
This is true only for Win9x. On NT based OS's bat files are not executed by command.com and can handle anything that windows can.
Post Reply