Announcement

Collapse
No announcement yet.

Renaming a running .EXE

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Renaming a running .EXE

    I recall that this has been discussed but despite much searching, can find no mention in this forum. Hopefully the code comments are self explanatory in terms of what I'm trying to do.

    Has anyone a simple solution?

    Code:
    ' wigglewoo.bas
    
    #COMPILE EXE
    #INCLUDE"win32api.inc"
    FUNCTION PBMAIN () AS LONG
    
    
    
    
    '  Now do what the program does all OK in here.
    '  When user choose to 'Exit' this program, comes to here.
    MSGBOX"Now ready to exit wigglewoo.exe..."
    
    
    
    ExitProgram:
    '--- Won't put full code in here but this section sucessfully ftp's the latest
    '    version of wigglewoo.ex2 from our website onto this PC.
    
    '--- Now, see if wigglewoo.exe & wigglewoo.ex2 are the same size.
    '    - if they are, the upgrade is not necessary.
    '    - if they are not then wigglewoo.ex2 is the latest
    '      version and needs to be renamed to wigglewoo.exe
    
    LOCAL LF1 AS LONG
    LOCAL LF2 AS LONG
    OPEN "wigglewoo.exe"AS #1:LF1=LOF(1):CLOSE #1'  get filesize of local version
    OPEN "wigglewoo.ex2"AS #2:LF2=LOF(2):CLOSE #2'  get filesize of ftp'd version
    IF LF1=LF2 THEN GOTO AllDone:'                  same size so no need to rename
    
    
    '--- OK, file sizes are different so need to nuke wigglewoo.exe and then rename
    '    wigglewoo.ex2 to wigglewoo.exe
    
    ' How to do this whilst this program (wigglewoo.exe) is still running?
    ' Windows returns a 'File locked' status on wigglewoo.exe
    
    ' How to exit this program and run into a
    ' SHELL "wigglewoo.bat" file?
    
    '??? create a .BAT file and run it off the end of here
    
    AllDone:
    END FUNCTION

  • #2
    Owen,
    I thought, while the EXE is running, that you could just use "Name x.EXE as y.EXE". I think I've done that in some of the Online Update code I've posted. I'll go find it and see for sure what I did. Got to deliver a grandkid to school first.

    Comment


    • #3
      Originally posted by Gary Beene View Post
      Owen,
      I thought, while the EXE is running, that you could just use "Name x.EXE as y.EXE". I think I've done that in some of the Online Update code I've posted. I'll go find it and see for sure what I did. Got to deliver a grandkid to school first.
      Not, the running .EX comes back with "File Locked". Am off to bed now so will hop back in in 6 hours. We're ahead in NZ!!!

      Comment


      • #4
        Self Modifying code (Discussion) 2012

        Restart an Application - Discussion 2011

        Auto update exe 2006
        Last edited by Pierre Bellisle; 18 May 2018, 07:02 PM.

        Comment


        • #5
          Originally posted by Pierre Bellisle View Post
          Ah that's starting to look hopeful Pierre - many thanks, will digest in the morning.

          Comment


          • #6
            Owen!

            Just tried it, and it works for me. Ran a "test.exe" and renamed it "new.exe" during execution. No error and the renamed file is found in Explorer as expected.

            Code:
            'Compilable Example:
            #Compiler PBWin 10
            #Compile Exe  "test.exe"
            #Dim All
            
            #Debug Display On
            #Debug Error On
            
            %Unicode = 1
            #Include "Win32API.inc"
            
            Enum Equates Singular
               IDC_Button
            End Enum
            
            Global hDlg As Dword
            
            Function PBMain() As Long
               Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
               Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
               Dialog Show Modal hDlg Call DlgProc
            End Function
            
            CallBack Function DlgProc() As Long
               Select Case Cb.Msg
                  Case %WM_Command
                     Select Case Cb.Ctl
                        Case %IDC_Button
                           Name "test.exe" As "new.exe"
                     End Select
               End Select
            End Function

            Comment


            • #7
              You are not renaming a running file here, you are making a copy of the disk file. If you attempt to modify the disk file of the running exe, the OS will prevent it. If there is some purpose to it, you can copy the original disk file to another name, start the copied file and from it, close down the original using inter app communication the delete it but it will be a bit complicated.
              hutch at movsd dot com
              The MASM Forum

              www.masm32.com

              Comment


              • #8
                Like you have discovered the running program on disk can be renamed as Owen described.

                This function allows creating new program and shellexecute using technique similar or same to what Steve has described.
                It does not kill previous versions on disk.
                Previous version can be kept running or terminated using EndCurrentProgram parameter.

                I thought you might want to modify the running program and shell to it.
                It was written to be a sub or function so some code can be eliminated.
                .
                Code:
                #COMPILE EXE
                #DIM ALL
                FUNCTION PBMAIN () AS LONG  'RenameAndRestart
                 LOCAL errCode,EndCurrentProgram AS LONG, sNewProgramName AS STRING
                 EndCurrentProgram = 1 'if 0 current program continues (just shellexecutes)
                 sNewProgramName = INPUTBOX$("New name",EXE.FULL$,"test1.exe")
                 RenameAndRestart(sNewProgramName,EndCurrentProgram,errCode)
                 ? "Error" + STR$(errcode),,"Ending " + EXE.FULL$
                ND FUNCTION
                
                FUNCTION RenameAndRestart(sNewProgramName AS STRING,EndCurrentProgram AS LONG,errCode AS LONG) AS LONG
                 IF LEN(sNewProgramName) = 0 THEN errCode = -1:FUNCTION = -1:EXIT FUNCTION
                 LOCAL hFile AS LONG
                 LOCAL pid AS DWORD
                 LOCAL sCurrentProgramName AS STRING
                 LOCAL sMem AS STRING
                
                 sCurrentProgramName = EXE.FULL$
                
                 hFile = FREEFILE
                 OPEN sCurrentProgramName FOR BINARY AS hFile
                 IF ERR THEN errCode = ERR:FUNCTION = ERR:EXIT FUNCTION
                 GET$ #hFile,LOF(hFile),sMem
                 IF ERR THEN
                  Errcode = ERR
                  FUNCTION = ERR
                  CLOSE #hFile
                  EXIT FUNCTION
                 END IF
                 CLOSE #hFile 'was missing
                 hFile = FREEFILE
                 OPEN sNewProgramName FOR OUTPUT AS hFile
                 IF ERR THEN
                   errcode = ERR
                   FUNCTION = ERR
                   EXIT FUNCTION
                 END IF
                 PRINT #hFile,sMem;
                 IF ERR THEN
                  errcode = ERR
                  FUNCTION = ERR
                  CLOSE #hfile
                  EXIT FUNCTION
                 END IF
                 CLOSE #hFile
                 pid = SHELL(sNewProgramName,1)
                 IF pid = 0 THEN errcode = -99:FUNCTION = -99:EXIT FUNCTION 'shell failed
                 IF EndCurrentProgram THEN
                  END 0
                 END IF
                END FUNCTION
                https://www.tesla.com/roadster

                Comment


                • #9
                  Hi Owen

                  If you wish to have a self deleting program as well, then use the following code but use it with care please!
                  The following program can self destruct

                  Code:
                  ' Program that can delete itself in a hidden fashion
                  
                  #COMPILE EXE "Self Destructor.exe"
                  #REGISTER NONE
                  #DIM ALL
                  #INCLUDE "WIN32API.INC"
                  
                  
                  %IDC_Button= 300
                  
                  GLOBAL hDlg AS DWORD
                  
                  
                  
                  '=============================
                  FUNCTION PBMAIN() AS LONG
                     DIALOG NEW PIXELS, 0, "Self Destructor",300,300,250,120,_
                       %WS_OVERLAPPEDWINDOW TO hDlg
                  
                     DIALOG SET COLOR hdlg,%RGB_YELLOW , %RGB_AZURE
                     CONTROL ADD BUTTON, hDlg, %IDC_Button," Press to self destruct ",_
                         60,40,120,20
                  
                     DIALOG SHOW MODAL hDlg CALL DlgProc
                  END FUNCTION
                  
                  
                  '============================
                  CALLBACK FUNCTION DlgProc() AS LONG
                     SELECT CASE CB.MSG
                        CASE %WM_COMMAND
                           SELECT CASE CB.CTL
                              CASE %IDC_Button
                                  SelfDestruct()
                                  DIALOG END  hDlg
                           END SELECT
                     END SELECT
                  END FUNCTION
                  
                  
                  
                  
                  
                  '======================================
                  ' Function to delete the  program itself
                  ' from the harddrive
                  ' This function must be placed at the END
                  ' of its run just before it exits the system
                  FUNCTION SelfDestruct() AS LONG
                   LOCAL szFile AS ASCIIZ *%MAX_PATH, _
                          szCmd AS ASCIIZ *%MAX_PATH
                  
                  
                   ' This shell deletion command would be hidden
                   IF GetModuleFileName(BYVAL 0&,szFile,%MAX_PATH) <> 0 AND _
                      GetShortPathName (szFile,szFile,%MAX_PATH)   <> 0      THEN
                  
                        szCmd= "/c del "+szFile+" >> NUL" + CHR$(0)
                        IF GetEnvironmentVariable("ComSpec",szFile,%MAX_PATH)<>0 _
                           AND _
                          ShellExecute(0,BYVAL 0,szFile,szCmd,BYVAL 0,%SW_HIDE)>32  THEN
                  
                             FUNCTION =%TRUE
                             EXIT FUNCTION
                        END IF
                  
                   END IF
                   FUNCTION = %FALSE
                  END FUNCTION

                  Comment


                  • #10
                    Pray, sir, what are you trying to accomplish that you think renaming an executing EXE file is the way to get there:?

                    Application inadequately described to suggest anything.

                    Code snippet suggests you are just trying to get the version of Wigglewoo.exe and then dependijng on which version it is, do "soemthing"

                    Use your program version resource to get the version... here's some functions I use, one for "this" program and one for "any"executable ...

                    Code:
                    ' Get version string of current Executable. Use FileVersionString to get for another file.
                    ' return program version string from resource file as "Major.minor build nnnn"
                    ' changed 5/27/03 to return major.minor.build, build used to be format$=0000, now it's just
                    ' a number. e.g., was "2.2 Build 0003" now is "2.2.3"
                    ' STANDARD PROGRAM VERSION STRING  format: major.minor.build
                    
                    FUNCTION ProgramVersionString() AS STRING
                    
                      LOCAL major AS LONG, Minor AS LONG, Build AS LONG
                      LOCAL szFile AS ASCIIZ * %MAX_PATH
                      LOCAL ResSize AS LONG
                      LOCAL ffi  AS VS_FIXEDFILEINFO PTR
                    
                      LOCAL  ret AS LONG
                      LOCAL Buffer AS STRING, DLLDate AS STRING
                    
                      Major=0: Minor = 0:  Build = 0
                    
                      GetModuleFileName BYVAL %NULL, szFile, SIZEOF(szFile)
                      ResSize = GetFileVersionInfoSize (szFile, ret)
                      IF ResSize= 0 THEN
                        FUNCTION = "Error"
                        EXIT FUNCTION
                      END IF
                    
                      Buffer    = SPACE$(ResSize)
                      Ret       = GetFileVersionInfo(szFile, %NULL, ResSize, BYVAL STRPTR(Buffer))
                    
                    
                    
                    ' ** Read the VS_FIXEDFILEINFO info
                      VerQueryValue BYVAL STRPTR(Buffer), "\", ffi, SIZEOF(@ffi)
                      Major      = @ffi.dwProductVersionMs  \ &h10000
                      Minor      = @ffi.dwProductVersionMs MOD &h10000
                      Build      = @ffi.dwProductVersionLS
                    
                      FUNCTION = LTRIM$(STR$(Major)) & "." & TRIM$(STR$(Minor)) & "." & LTRIM$(STR$(Build))
                    
                    END FUNCTION
                    
                    
                    ' get version of any file
                    ' removed leading space from major 8/24/07
                    FUNCTION FileVersionString(szFile AS ASCIIZ) AS STRING
                      LOCAL major AS LONG, Minor AS LONG, Build AS LONG, SubBuild AS LONG
                      LOCAL ResSize AS LONG
                      LOCAL ffi  AS VS_FIXEDFILEINFO PTR
                      LOCAL  ret AS LONG
                      LOCAL Buffer AS STRING, DLLDate AS STRING
                    
                      Major=0: Minor = 0:  Build = 0
                    
                      ResSize = GetFileVersionInfoSize (szFile, ret)
                      IF ResSize= 0 THEN
                        FUNCTION = "No Version Info"
                        EXIT FUNCTION
                      END IF
                    
                      Buffer = SPACE$(ResSize)
                      Ret =  GetFileVersionInfo(szFile, %NULL, ResSize, BYVAL STRPTR(Buffer))
                    
                    ' ** Read the VS_FIXEDFILEINFO info
                      VerQueryValue BYVAL STRPTR(Buffer), "\", ffi, SIZEOF(@ffi)
                      Major        = @ffi.dwProductVersionMs  \ &h10000
                      Minor        = @ffi.dwProductVersionMs MOD &h10000
                      Build        = @ffi.dwProductVersionLS MOD &h10000  ' this is for MY software which uses VERSION_MAJOR, VERSION_MINOR, 0, VERSION_BUILD under FILEVERSION
                    '  SubBuild     = @ffi.dwProductVersionLS \  &h10000
                      ' combine
                    
                    ' 8/24/07 WAS:
                      FUNCTION = STR$(Major) & "." & TRIM$(STR$(Minor)) & "." & LTRIM$(STR$(Build))   ' & "." & LTRIM$(STR$(SubBuild))
                    ' changed to: (to remove leading space)
                      FUNCTION = FORMAT$(Major) & "." & FORMAT$(Minor) & "." & FORMAT$(Build)   ' & "." & FORMAT$(SubBuild)
                    
                    
                    END FUNCTION
                    NOTE NON-STANDARD USE OF MAJOR/MINOR/BUILD/SUBBUILD WHEN I CREATE THE STRING! .

                    ERROR

                    MCM
                    Last edited by Michael Mattias; 18 May 2018, 03:49 PM. Reason: Changed "Pray, madam" to "Pray, sir" in first line. I had thoght Ms. Wilson was the OP rather than the correct Mr. English

                    Comment


                    • #11
                      Hey Steve,

                      You are not renaming a running file here, you are making a copy of the disk file. If you attempt to modify the disk file of the running exe, the OS will prevent it.
                      Are you talking about the code I posted?

                      When I run it and look in the folder, all that is left is the renamed EXE. There is no file there by the original name. How does that match your comment about just making a copy?

                      Comment


                      • #12
                        I am interested

                        But why would you want to do this?

                        Kerry
                        [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                        Kerry Farmer

                        Comment


                        • #13
                          Kerry,
                          I did it as a way of allowing the download of a new version of an EXE, from within the running EXE, without requiring a second EXE being involved. That seems to be Own is doing too.

                          Owen,
                          I know I've posted code for doing an online update of an EXE. Did you find that and did it fail for you?

                          Comment


                          • #14
                            Hi Gary,

                            I originally answered Owen's question but I just tested your code and it works fine. Seems renaming a running exe is not critical like deleting a running exe. I can do the same rename while the file is running in Winfile.exe so its not problematic to rename a running file. You can also copy a running exe.
                            hutch at movsd dot com
                            The MASM Forum

                            www.masm32.com

                            Comment


                            • #15
                              When I run it and look in the folder, all that is left is the renamed EXE. There is no file there by the original name.
                              I think that this "worked" was neophyte's serendipity.

                              MCM

                              Comment


                              • #16
                                MCM,
                                It's worked that way since Win 2000, according to what I've read. I've used it for the last decade. Perhaps you should read more before you bark.

                                Comment


                                • #17
                                  If using rename method sounds fine as long as the old program ends.
                                  Could cause problems if database format changes and old program is writing in a different format?
                                  Curious if shortcut (.lnk) to executable gets renamed twice?

                                  I currently download a setup program (without using a browser.)
                                  pid = SHELL(InnoSetup.exe /silent mode,1) 'start new version
                                  END

                                  I'll have to test the rename running program method on a network.
                                  https://www.tesla.com/roadster

                                  Comment


                                  • #18
                                    Originally posted by Steve Hutchesson View Post
                                    You are not renaming a running file here, you are making a copy of the disk file. If you attempt to modify the disk file of the running exe, the OS will prevent it. If there is some purpose to it, you can copy the original disk file to another name, start the copied file and from it, close down the original using inter app communication the delete it but it will be a bit complicated.
                                    No, I wanted to delete the running program and then rename the second one with the running programs name. In other words, nuke the .exe and rename the .ex2 to .exe. Seems like a simple way to install an upgraded .exe.

                                    Comment


                                    • #19
                                      Originally posted by Gary Beene View Post
                                      Owen!

                                      Just tried it, and it works for me. Ran a "test.exe" and renamed it "new.exe" during execution. No error and the renamed file is found in Explorer as expected.

                                      Code:
                                      'Compilable Example:
                                      #Compiler PBWin 10
                                      #Compile Exe "test.exe"
                                      #Dim All
                                      
                                      #Debug Display On
                                      #Debug Error On
                                      
                                      %Unicode = 1
                                      #Include "Win32API.inc"
                                      
                                      Enum Equates Singular
                                      IDC_Button
                                      End Enum
                                      
                                      Global hDlg As Dword
                                      
                                      Function PBMain() As Long
                                      Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
                                      Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
                                      Dialog Show Modal hDlg Call DlgProc
                                      End Function
                                      
                                      CallBack Function DlgProc() As Long
                                      Select Case Cb.Msg
                                      Case %WM_Command
                                      Select Case Cb.Ctl
                                      Case %IDC_Button
                                      Name "test.exe" As "new.exe"
                                      End Select
                                      End Select
                                      End Function
                                      Thanks Gary - this works a treat and is not impeded by W10 preventing the delete then rename. Thanks everyone else...... this is a nice simple way to run auto-upgrades.

                                      Comment


                                      • #20
                                        No, I wanted to delete the running program and then rename the second one with the running programs name
                                        I wonder if that "works" if the program had to be swapped out and all or part of it is currently stored in the page file. It never happens with these tiny little test files, but if the (real) program is big enough and the system busy enough....??????

                                        MCM

                                        Comment

                                        Working...
                                        X