• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Self-upgrading program...

I'm looking for a way to have my program be able to upgrade itself!  Using D4, I want to have my program check my website for a newer version of itself, download the newer version, and then terminate itself, start the upgrade process, and start itself over as the newer version!

Now... I can already do the "check the website and download the newer version" stuff, and I can figure out how to do the second part (unzipping the upgrade, installing and running the new version), say via a batch file.  The second process, especially if it's a batch file as I'm envisioning, can easily start the "new" program over again, but I need to know HOW to do that all-important first step!

HOW do I tell my program to stop THEN tell it to run the batch file (seems very counter-intuitive...)???

Any and all help appreciated!

Jim 8^)
0
Raven1155
Asked:
Raven1155
  • 5
  • 4
  • 3
  • +4
1 Solution
 
edeyCommented:
You may want to write a small proggy that gets called by your app.  After starting this small app could send the upgrading app a msg to shut down, wait for confirmation and then run your batch.


GL
Mike
0
 
EpsylonCommented:
Hi Jim, the code below is from another question I answered and I think it's worth trying.

It has a button which can be clicked on when there is a new version available at 'C:\Update\Project1.exe'.

When the client is running an old version and is ready to upgrade (Button1 simulates this in my example), the old version starts an instance of the new version and terminates itself. The new version deletes the old one, copies itself to the original directory and runs the new version. The first instance of the new version terminates leaving the second instance running.


uses ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
const NEWEXE = 'c:\update\project1.exe';
var path, arg, exe: String;
begin
  path := '"' + ExtractFilePath(ParamStr(0)) + '"';
  arg := '"' + ParamStr(0) + '"';
  exe := '"' + NEWEXE + '"';
  ShellExecute(0, 'open', PChar(exe), PChar('-u ' + arg) , PChar(path), SW_SHOW);
  Application.Terminate;
end;
       
procedure TForm1.FormCreate(Sender: TObject);
var path, exe: String;
    i: Integer;
begin
  if (ParamCount > 1) and (ParamStr(1) = '-u') then
  begin
    i := 0;
    while (not DeleteFile(ParamStr(2))) and (i < 20) do
    begin
      Sleep(500);
      Inc(i);
    end;
    if i < 20 then
    begin
      CopyFile(PChar(ParamStr(0)), PChar(ParamStr(2)), false);
      path := '"' + ExtractFilePath(ParamStr(2)) + '"';
      exe := '"' + ParamStr(2) + '"';
      ShellExecute(0, 'open', PChar(exe) , '', PChar(path), SW_SHOW);
    end
    else
      ShowMessage('Upgrade failed due to timeout');
    Application.Terminate;
  end;
end;
0
 
d4jaj1Commented:
Do it in this fashion,

1)  Check for new version of exe (you are already doingg this)
2)  Create a Batch file via code to delete the old exe and copy and/or rename the new exe in its place.  Use the "Loop" command to make teh batch file attempt to delete the exe continuously until it succeeds.  the final lines in teh bat file should run teh new exe and delete itself.  I can provide code if you like.
3) At the point where you want to update the exe (like after the version checking), execute the batch file, THEN call Application.terminate.  This way, the batch file continuesly attempts to delete/copy-rename the exe - but also allows you to terminate the old application in order to overwrite it.

If you need code examples, I can provide.  Let me know.

Jay
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
GwenaCommented:
Lets say your application is named 'myapp.exe'
And your new upgraded version is called 'myapp-upgrade.exe'

Write a tiny program..'upgrader.exe'... that when run looks into it's own dir and if it finds 'myapp.exe' AND 'myapp-upgrade.exe' it will then delete 'myapp.exe' and then rename 'myapp-upgrade.exe' to 'myapp.exe'
then it could run the newly upgraded application and shut itself down.

Store the file 'upgrader.exe' inside a res file in your application so it can be written to disk and run when needed.

Have a routine inside your application that executes on startup.. if this routine finds 'upgrader.exe' in it's directory it deletes it.

So here is how it would go during a web update
---------------------------------

-You place 'myapp-upgrade.exe' on your web site
-Your application when run on the users machine fetches this new upgrade and places it in it's dir
-Your application spawns 'upgrader.exe' and writes it to disk in it's dir
and runs upgrader.exe and shuts itself down
-upgrader.exe finds myapp.exe AND myapp-upgrade.exe in it's dir so it deletes myapp.exe and renames the new upgrade to the original application name.
-upgrade.exe then runs the newly upgraded myapp.exe and shuts itself down
-the new myapp.exe runs and finds upgrader.exe in it's directory so it deletes it
;-)
 
0
 
umuligCommented:
There is a free comp that can do this for you at
www.tmssoftware.com

Regards
Umulig
0
 
EpsylonCommented:
Jay, I think you should withdraw your answer since you are not the first one and your answer lacks an example.
0
 
d4jaj1Commented:
Epsylon,

As you can see from the time stamp, we appraently attempted to answer the question at the same time.  As for withdrawning the answer, (unless I'm missing something) Raven can accept your comment as the answer instead of mine.  Finally, Raven stated he already knew how to check for the version and create teh batch file.  Hios question really was " HOW do I tell my program to stop THEN tell it to run the batch file " - not , how do I create a program from scratch to do it for me.  All the code he needs to answer the question she asked is what I submitted.

Thanks for the thought.
0
 
d4jaj1Commented:
oops, mis-typed 'She' instead of "He'.  I aplogize Jim.  Also, I stated I could provide code.
0
 
EpsylonCommented:
>Also, I stated I could provide code.

What are you waiting for? Your answer is not an answer yet...
0
 
d4jaj1Commented:
Again, read the question.  Jim stated he already knew how to do the 1st & last parts of the procedure.  This implies he has the code for this.  The only code thats necessary is what I provided and suggested when he should call it.

Now, that's my answer.  Shut Up!  If the customer has questions - let him ask.
0
 
edeyCommented:
a little hostile d4jaj1.  Esp. considering your "answer" isn't particularily elegant (and relies on non delphi tech to boot), certainlynot any better then the suggestion from my comment posted .5 hour before yours.  Lighten up.


GL
Mike
0
 
Raven1155Author Commented:
Jay,

Please post an example of the Batch code (I'm unfamiliar with the Loop syntax though I'm sure I know what it does)!  After that I'll grade the answer...

Gwena- Although I would have to read up on how to "store" a mini-program in a .RES file, your answer still doesn't tell me how I get the mini-program to run AND kill the main program at the same time (maybe, probably, I'm missing something here conceptually)...

Edey, Epsylon, Gwena, & Umulig- I appreciate your comments/answers.  I'll provide points appropriately!

Jim 8^o

0
 
bryan7Commented:
listenning
0
 
Raven1155Author Commented:
..waiting...
0
 
GwenaCommented:
Hi Raven1155

I hope my comment is at least a little bit helpful.... but don't worry about any points for me... I don't really collect them... I just make comments for the fun of it :-)
0
 
Raven1155Author Commented:
Rejected as only a partial answer!

Said he'd provide example of batch code, even chewing out another "expert" about it, and hasn't done so after 3 days.

I will delete this question and post questions to provide points to all others involved.
0
 
Raven1155Author Commented:
Accepted as answer since it's the easiest solution (less work for me), but not to say that it's otherwise any better than the others...

Couldn't figure out how to delete the question anyway! ;^o
0
 
d4jaj1Commented:
Jim,

Sorry about not posting the code, I was out of town.  As for the points, thats fine, as we don't get anything for having the highest points.  So, I will work with you until you get what you need without the points.

So, here gos.


// Code you use to determine if exe needs to be replaced

if {whatever you currently do} < [NewVersion] then
 begin
  updateexe;  //creates Batch File
  WinExec(batfilename, SW_HIDE);  // Runs it hidden
  application.Terminate;  // closes your application

Procedure tform1.UpdateEXE;
var
 batfile : TextFile;
 myapp: string;
begin
  myapp := application.exename;
  batfilename := 'c:\UPDATEEXE.BAT';
  AssignFile(batfile, batfilename);
  Rewrite(batfile);
  WriteLn(batfile, '@echo off');
  WriteLn(batfile, ':TryDelete');
  WriteLn(batfile, ' del ' + '"' + myapp + '"');
  WriteLn(batfile, ' if exist ' + '"' + myapp + '"' + ' goto TryDelete');
***  WriteLn(batfile, ':TryCopy');
***  WriteLn(batfile, ' Copy "\\ServerName\Share\NewExeName.exe" "' + myapp + '"');
***  WriteLn(batfile, ' if not exist ' + '"' + myapp + '"' + ' goto TryCopy');
  WriteLn(batfile, '"' + myapp + '"');  
  WriteLn(batfile, 'del ' + batfilename);
  CloseFile(batfile);
end;
 end;

Basically, the Update EXe procedure creates a batch file, assigns a name to it, tries to delete the 'old' file continuosly, copies the new file in its place, runs it then deletes itself.  The lines marked with *** are what I use to copy the file since our exe's are on a network share instead of the web.  You may wish to modify or delete these to lines if you already have a method for copying the exe from the web.

I've tested this on Win9x and WinNT so that shouldn't be a problem.  Hopefully this is what you are looking for, if not, please let me know.  

Jay
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 5
  • 4
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now