Page 1 of 1

Posted: Sun Jan 19, 2003 6:22 pm
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

Posted: Wed Jan 22, 2003 2:59 am
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! :)

Posted: Wed Jan 22, 2003 10:46 pm
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...

Posted: Mon May 26, 2003 11:23 am
by darklordz
Whynot just do getshortfilename from windows api, it returnd the dos filename..../path aka 6chrs~1

Posted: Mon Sep 25, 2006 1:55 am
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 "?

Posted: Mon Sep 25, 2006 1:10 pm
by Trond
If you use " you end the string.

Posted: Mon Sep 25, 2006 3:22 pm
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

Posted: Thu Oct 26, 2006 5:42 pm
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.

Re: Win32 - Delete running application (uninstall)

Posted: Mon Oct 05, 2009 2:52 pm
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) 

Re: Win32 - Delete running application (uninstall)

Posted: Tue Oct 06, 2009 12:20 am
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.