Solved

Self-upgrading program...

Posted on 2000-04-05
18
254 Views
Last Modified: 2010-04-06
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
Comment
Question by:Raven1155
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +4
18 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2687602
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2687671
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
 
LVL 3

Expert Comment

by:d4jaj1
ID: 2687674
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Gwena
ID: 2687716
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
 
LVL 1

Accepted Solution

by:
umulig earned 200 total points
ID: 2689061
There is a free comp that can do this for you at
www.tmssoftware.com

Regards
Umulig
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2689099
Jay, I think you should withdraw your answer since you are not the first one and your answer lacks an example.
0
 
LVL 3

Expert Comment

by:d4jaj1
ID: 2690132
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
 
LVL 3

Expert Comment

by:d4jaj1
ID: 2690141
oops, mis-typed 'She' instead of "He'.  I aplogize Jim.  Also, I stated I could provide code.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2690199
>Also, I stated I could provide code.

What are you waiting for? Your answer is not an answer yet...
0
 
LVL 3

Expert Comment

by:d4jaj1
ID: 2690952
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
 
LVL 6

Expert Comment

by:edey
ID: 2691104
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
 

Author Comment

by:Raven1155
ID: 2691616
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
 
LVL 3

Expert Comment

by:bryan7
ID: 2691867
listenning
0
 

Author Comment

by:Raven1155
ID: 2694969
..waiting...
0
 
LVL 5

Expert Comment

by:Gwena
ID: 2697300
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
 

Author Comment

by:Raven1155
ID: 2698224
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
 

Author Comment

by:Raven1155
ID: 2698231
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
 
LVL 3

Expert Comment

by:d4jaj1
ID: 2700767
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question