Date: 02-16-2022
Return to Index
created by gbSnippets
'Credit to Pierre Bellisle
'The new EXE
#Compile EXE "NewExe.exe" '#Win 8.04#
#Dim All
%Unicode=1
#Include "WIN32API.INC"
Function PBMAIN() As Long
MessageBox(%HWND_DESKTOP, "I am the new exe.", "Exe Auto Update", %MB_ICONINFORMATION OR %MB_OK)
End Function
'Compilable Example: (Jose Includes)
'The main app that create
#Compiler PBWin 9, PBWin 10
#Compile EXE "AutoUpdateExe" '#Win 8.04#
#Register None
#Include "Win32Api.inc"
OPTION EXPLICIT
Global hDlg AS DWord
%LabelInfo = 101
%ButtonBat = 201
'______________________________________________________________________________
CALLBACK Function DlgProc
Local zPathExeName As AsciiZ * %MAX_PATH
Local sExeName As String
Local sNewExe As String
Local sBatFile As String
Local hFile AS DWord
Local BsPos As Long
SELECT CASE CBMSG
CASE %WM_COMMAND
SELECT CASE LOWRD(CBWPARAM)
CASE %ButtonBat
IF CBCTLMSG = %BN_CLICKED THEN
GetModuleFileName(ByVal 0, zPathExeName, %MAX_PATH) 'Get current exe name and path
BsPos = INSTR(-1, zPathExeName, "\")
sExeName = MID$(zPathExeName, 1 + BsPos) 'Remove path
sExeName = $DQ & sExeName & $DQ 'Current exe name with quote
sNewExe = $DQ & "NewExe.exe" & $DQ 'Name of the new updated exe
sBatFile = "AutoUpdateExe.bat" 'Name of the temporary bat file
hFile = FREEFILE
OPEN sBatFile FOR OUTPUT AS hFile 'Create the batch file
PRINT # hFile, ":Loop" 'Label for looping
PRINT # hFile, "Attrib " & sExeName & "-R -S -H" 'Attrib
PRINT # hFile, "Del " sExeName 'Delete our exe
PRINT # hFile, "If exist " & sExeName & " goto Loop" 'This will loop until our exe end's so it can be deleted
PRINT # hFile, "Rename " & sNewExe & " " & sExeName 'Rename to new exe
PRINT # hFile, sExeName 'Rerun exe
PRINT # hFile, "Del " & sBatFile; 'Batch erase itself, note the last ";"
CLOSE # hFile
ShellExecute(%HWND_DESKTOP, "Open", BYCOPY sBatFile, "", "", %SW_HIDE) 'Run the batch file
DIALOG END CBHNDL 'and exit
END IF
END SELECT
END SELECT
End Function
'______________________________________________________________________________
Function PBMAIN()
IF LEN(DIR$("NewExe.exe")) = 0 THEN
MSGBOX "Please first compile the file ""NewExe.exe"" to make the AutoUpdate possible", 64,"AutoUpdate"
ELSE
DIALOG NEW %HWND_DESKTOP ,"Exe Auto Update", , , 170, 70, %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO hDlg
SetClassLong hDlg, %GCL_HICON, LoadIcon(ByVal %NULL, ByVal %IDI_INFORMATION)
CONTROL ADD LABEL, hDlg, %LabelInfo, "This exe will create a bat file," & $CRLF & _
"execute it and exit." & $CRLF & _
"The bat file will delete current exe," & $CRLF & _
"replace it with new one and execute it.", 5, 5, 190, 32
CONTROL ADD BUTTON, hDlg, %ButtonBat, "Update", 35, 45, 100, 15
DIALOG SHOW MODAL hDlg CALL DlgProc
END IF
End Function
'______________________________________________________________________________
'
'gbs_00719
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm