Second Idea: (just coded, not tested)
Maybe this is of any help
Thank you in advance for your feedback!
Code: Select all
; ---------------------------------------------------------------------------------------------------------------------
;
; Updater.exe 0.00
;
; call the update with
; RunProgram("Updater.exe", "<ExeFile> <ServerFile> [/silent]", "")
;
; Parameter:
;
; ExeFile .. executable file to be updated (with path or should be in the same directory as the updater)
;
; ServerFile .. Location of the new file (https://....)
;
; bSilent .. /silent otherwise MessageRequester shows errors
;
; ---------------------------------------------------------------------------------------------------------------------
EnableExplicit
#ProgramName$ = "Updater"
#ErrorCaption$ = #ProgramName$ + " Error"
#InfoCaption$ = #ProgramName$ + " "
; ---------------------------------------------------------------------------------------------------------------------
Procedure GetParentPID()
Protected hSnapshot
Protected PROCESSENTRY32.PROCESSENTRY32
Protected bProcess
hSnapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, #Null)
If hSnapshot
PROCESSENTRY32\dwsize = SizeOf(PROCESSENTRY32)
bProcess = Process32First_(hSnapshot, @PROCESSENTRY32)
While bProcess
If PeekS(@PROCESSENTRY32\szExeFile, #MAX_PATH) = GetFilePart(ProgramFilename())
CloseHandle_(hSnapshot)
; Debug "ProcessID = " + PROCESSENTRY32\th32ProcessID
; Debug "Parent ProcessID = " + PROCESSENTRY32\th32ParentProcessID
ProcedureReturn PROCESSENTRY32\th32ParentProcessID ; return the found parent process id
EndIf
bProcess = Process32Next_(hSnapshot, @PROCESSENTRY32)
Wend
CloseHandle_(hSnapshot)
EndIf
ProcedureReturn 0 ; no parent process id found
EndProcedure
; ---------------------------------------------------------------------------------------------------------------------
Procedure.s GetProcessPath(PID)
Protected hSnapshot
Protected PROCESSENTRY32.PROCESSENTRY32
Protected bProcess
hSnapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, #Null)
If hSnapshot
PROCESSENTRY32\dwsize = SizeOf(PROCESSENTRY32)
bProcess = Process32First_(hSnapshot, @PROCESSENTRY32)
While bProcess
If PROCESSENTRY32\th32ProcessID = PID
CloseHandle_(hSnapshot)
ProcedureReturn PeekS(@PROCESSENTRY32\szExeFile) ; return the parent executable file name
EndIf
bProcess = Process32Next_(hSnapshot, @PROCESSENTRY32)
Wend
CloseHandle_(hSnapshot)
EndIf
ProcedureReturn "" ; no parent process found
EndProcedure
; ---------------------------------------------------------------------------------------------------------------------
Procedure Main()
Protected ExeFile$, DownloadFile$, BakFile$
Protected ExeName$, ServerFile$, path$
Protected bSilent
; read the agruments (quick and dirty)
;
ExeFile$ = ProgramParameter() ; executable file to be updated
ServerFile$ = ProgramParameter() ; i.e. "https://,,,,"
bSilent = Bool(ProgramParameter() = "/silent")
; if ExeFile$ is empty, updater.exe probably called by double click directly
;
If ExeFile$ = ""
; Not bSilent
MessageRequester(#ErrorCaption$, #ProgramName$ + " is not a stand-alone program!")
ProcedureReturn 1 ; some error
EndIf
path$ = GetPathPart(ExeFile$)
If path$ = ""
path$ = GetPathPart(ProgramFilename()) ; Updater and Executable are in the same directory !!!
EndIf
ExeName$ = GetFilePart(ExeFile$, #PB_FileSystem_NoExtension)
BakFile$ = path$ + ExeName$ + ".bak"
DownloadFile$ = path$ + ExeName$ + "_New.exe" ; temporary name for downloaded file
; do the download in some way
;
If Not ReceiveHTTPFile(ServerFile$, DownloadFile$) ;
If Not bSilent
MessageRequester(#ErrorCaption$, "Update failed!")
EndIf
ProcedureReturn 1 ; some error
EndIf
; Check if current executable is terminated already
;
While GetParentPID()
Debug "Wait until the exe is finished" ; show debug message while waiting
Delay(2000)
Wend
; make a backup of current executable (remove the last one)
;
DeleteFile(BakFile$) ; maybe no backup file (first update)
If RenameFile(ExeFile$, BakFile$)
If RenameFile(DownloadFile$, ExeFile$)
If Not bSilent
MessageRequester(#InfoCaption$, "Update successful", 0)
EndIf
RunProgram(ExeFile$, "", "")
EndIf
EndIf
ProcedureReturn 0
EndProcedure
End Main()