Delete your own Exe (For UnInstaller )

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

Delete your own Exe (For UnInstaller )

Post by Rings »

Code updated for 5.20+

Code: Select all

Procedure UninstallApp()
  sApp.s = Space(256)
  GetModuleFileName_(GetModuleHandle_(0), @sApp, 256)        
  MessageRequester("Info", "Hit me to delete myself..." + 
                            Chr(13) + sApp, 0)
  OpenFile(1, "~~uninst.bat")
  WriteStringN(1, "del " + GetFilePart(sApp))
  WriteStringN(1,"del ~~uninst.bat")
  CloseFile(1)
  RunProgram("~~uninst.bat")
EndProcedure            

UninstallApp()
SPAMINATOR NR.1
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Post by Le Soldat Inconnu »

This code don't work on windows XP.

It run on win 2000 or win98 but on win xp, the *.bat file is not correctly execute.

Someone has a solution ?

thanks.
LSI
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

this can

Post by thefool »

Thanks to his idea i did this. It works, and is much smaller.
Why use so many lines in the batch file?
Anyway, thanks for the api command giving you the filename.,

Code: Select all

;Get app's filename
sApp.s=Space(256) 
GetModuleFileName_(GetModuleHandle_(0), @sApp, 256)
;Little message  
MessageRequester("hello","Push this button to delete this app.")
appfile.s=GetFilePart(sApp.s) ;Get file part...
;Writing batch file...
OpenFile(1,"tmp.bat")
WriteStringN("del "+appfile.s)
WriteStringN("del tmp.bat")
CloseFile(1)
;Execute the file
RunProgram("tmp.bat")
;Done!
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Re: this can

Post by Berikco »

thefool wrote:Why use so many lines in the batch file?
Because its possible the batch file is finished before your program is ended. So the batch file cant delete a running program, a check in a loop is needed...
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: this can

Post by ricardo »

Berikco wrote:
thefool wrote:Why use so many lines in the batch file?
Because its possible the batch file is finished before your program is ended. So the batch file cant delete a running program, a check in a loop is needed...
Its almost secure that if you 'runprogram()', the bat will run when your app is open, you are right.

The first example runs fine here in XP pro.
ARGENTINA WORLD CHAMPION
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Post by Wayne Diamond »

Hrmmm. Rings didn't write this, he just ported my Powerbasic code that i wrote back in 2002:
http://www.powerbasic.com/support/forum ... 01350.html
Anden
Enthusiast
Enthusiast
Posts: 135
Joined: Mon Jul 21, 2003 7:23 am
Contact:

Post by Anden »

Without batch (could be troublesome with different OSes):

Create exe "RestartExe":

Code: Select all

exename.s = StringField(ProgramParameter(), 1, " ")
HomeDir.s = StringField(ProgramParameter(), 2, " ")
i.b = 0
While (DeleteFile(exename) = 0 And i <= 10)
  Delay(250) ;wait for old exe to quit
  i = i + 1
Wend
CopyFile(HomeDir+"Transfer\_#_2A-ChitChat_Upd.exe", exename)
DeleteFile(HomeDir+"Transfer\_#_2A-ChitChat_Upd.exe")
RunProgram(exename, "", GetPathPart(exename))
End
Include this in your main exe:

Code: Select all

DataSection
RestartExe: IncludeBinary "RestartExe.exe"
_RestartExe:
EndDataSection

Procedure.s MyExeName()
  File$ = Space(512)
  GetModuleFileName_(GetModuleHandle_(#NULL), @File$, 512)
  ProcedureReturn File$
EndProcedure

CreateFile(68, HomeDir+"RestartExe.exe")
WriteData(?RestartExe, ?_RestartExe-?RestartExe)
CloseFile(68)
If (MessageRequester("Restart 2A-ChitChat", "Received new client version "+text+" from server."+cr+cr+"Press <OK> to restart or <CANCEL> to quit program.", #MB_OKCANCEL|#MB_ICONQUESTION|#MB_TASKMODAL) = #IDOK) ;yes
  RunProgram(HomeDir+"RestartExe.exe", MyExeName()+" "+HomeDir, HomeDir, 2)
EndIf
Should work too (although it's somehow bloated :-))
Manne
User
User
Posts: 30
Joined: Mon Apr 28, 2003 8:49 pm

Post by Manne »

For nt-based systems there is an easier way.
common

Code: Select all

; PureBasic Visual Designer v3.82 build 1356
DefType.s Filename, tmp
NewList Dateien.s()
osvi.OSVERSIONINFO

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Listview_0
  #Button_0
  #Button_1
  #Button_2
  #Image_0
EndEnumeration

;- Statusbar Constants
;
Enumeration
  #StatusBar_0
EndEnumeration

;- Image Plugins
UseJPEGImageDecoder()

;- Image Globals
Global Image0

;- Catch Images
Image0 = CatchImage(0, ?Image0)

;- Images
DataSection
Image0:
  ;IncludeBinary "F:\sicherung\dottore.jpg"
EndDataSection

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 339, 204, 302, 236,  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered , "Delay File Delete 1.0")
    If CreateStatusBar(#StatusBar_0, WindowID())
      AddStatusBarField(302)
      StatusBarText(#StatusBar_0, 0, "Bereit")
    EndIf
    If CreateGadgetList(WindowID())
      ListViewGadget(#Listview_0, 10, 10, 180, 200, #LBS_SORT)
      ButtonGadget(#Button_0, 210, 10, 80, 30, "Auswählen")
      ButtonGadget(#Button_1, 210, 50, 80, 30, "Löschen")
      ButtonGadget(#Button_2, 210, 90, 80, 30, "Beenden")
      ;ImageGadget(#Image_0, 210, 130, 80, 80, Image0, #PB_Image_Border)
      
    EndIf
  EndIf
EndProcedure
main

Code: Select all

; PureBasic Visual Designer v3.82 build 1356

IncludeFile "Common.pb"

osvi\dwOSVersionInfoSize = SizeOf(OSVERSIONINFO)
GetVersionEx_(osvi)
If osvi\dwPlatformId <> #VER_PLATFORM_WIN32_NT Or osvi\dwMajorVersion = 4
  MessageRequester("Fehler", "Windows 9x/ME wird nicht unterstützt!", #MB_ICONSTOP)
  End
EndIf

Open_Window_0()
DisableGadget(#Button_1, 1)

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_EventCloseWindow
    quit = 1
  ElseIf Event = #PB_Event_Gadget
    Select EventGadgetID()
      Case #Listview_0
        
      Case #Button_0  ; Auswählen
        Filename = OpenFileRequester("Datei(en) auswählen", "C:\", "Alle Dateien (*.*)|*.*", 0, #PB_Requester_MultiSelection)
        Repeat
          AddElement(Dateien())
          Dateien() = Filename
          SendMessage_( GadgetID(#Listview_0), #LB_ADDSTRING, 0, GetFilePart(Filename))
          Filename = NextSelectedFileName()
        Until Filename = ""
        DisableGadget(#Button_1, 0)
        
      Case #Button_1  ; Löschen
        ForEach Dateien()
          SetFileAttributes_(Dateien(), #FILE_ATTRIBUTE_NORMAL)
          MoveFileEx_(Dateien(), #NULL, #MOVEFILE_DELAY_UNTIL_REBOOT)
        Next
        ClearGadgetItemList(#Listview_0)
        StatusBarText(#StatusBar_0, 0, "Dateien werden nach dem nächsten Neustart gelöscht!") 
         
      Case #Button_2  ; Beenden
        quit = 1
        Break
    EndSelect
  EndIf
    
Until quit
End
;
Manne
Post Reply