Solved

Self-upgrading program...

Posted on 2000-04-05
18
249 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
  • 5
  • 4
  • 3
  • +4
18 Comments
 
LVL 6

Expert Comment

by:edey
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:Gwena
Comment Utility
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
Comment Utility
There is a free comp that can do this for you at
www.tmssoftware.com

Regards
Umulig
0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
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
Comment Utility
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
Comment Utility
oops, mis-typed 'She' instead of "He'.  I aplogize Jim.  Also, I stated I could provide code.
0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
>Also, I stated I could provide code.

What are you waiting for? Your answer is not an answer yet...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Expert Comment

by:d4jaj1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
listenning
0
 

Author Comment

by:Raven1155
Comment Utility
..waiting...
0
 
LVL 5

Expert Comment

by:Gwena
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Firewall issues 6 84
Delphi Form ownership 4 50
HTML text in the body of an email (delphi code) 12 71
select query - oracle 16 81
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now