esk
asked on
Upgrade my program
Let's say if have have client running on remote machine and i like to send query to the client , to upgrade to newer version that the .exe file will be available on any network drive P:\ for instance. So the question is!
How can i upgrade this client, terminate the client and run new over the old one?
Esk
Use ShellExecute to start the new version from p:\ (maybe using an '/upgrade' parameter). Then the old version exits and the new version copies the new exe to the local drive. Then it start the local exe and exits leaving the local new exe running.
we have a centralised server application which runs here, called "flags". each application checks their respective flags status during the course of work (through a background thread) and when upgrades, or server work is required, the respective applications will be forced to exit when the flags status changes. this frees up your application executable for updates, however your users/administrators would still have to restart the application. obviously your applications flag setting will also need to be re-enabled prior to this restart otherwise it will just exit again, or as in our case, not be allowed to start.
Hi esk,
I've had a similar problem. My app. is running on a few local machines and I am able to update it using app. named Loader, written by me for this case. The idea is as follows. This Loader is started every time instead used application. It first looks on my computer (over network) and if there are some updates on my app. service directory, the Loader gets the files, overrwrites them on local machine and then starts client application. This helps supporting applications that are started periodically (i.e. once a day). If they are working continiously, then things are more dificult because of need to send/receive messages. Anyway in all cases you'll need Loader, I think.
Jo.
I've had a similar problem. My app. is running on a few local machines and I am able to update it using app. named Loader, written by me for this case. The idea is as follows. This Loader is started every time instead used application. It first looks on my computer (over network) and if there are some updates on my app. service directory, the Loader gets the files, overrwrites them on local machine and then starts client application. This helps supporting applications that are started periodically (i.e. once a day). If they are working continiously, then things are more dificult because of need to send/receive messages. Anyway in all cases you'll need Loader, I think.
Jo.
ASKER
i only want to send a string with mailslot that contains "upgrade client:", when the client program receive this message , he will terminate him self but before he will teminate i want to make .bat file that checks "is the application running" if not then copy from location p:\ to local. and the bat will run the program again and delete the .bat file
Esk
Esk
ASKER
can i have!
This example demonstrates the replacement of the exe. Button1 simulates the update request.
Uses ShellAPI;
const NEWEXE = 'p:\newexe\project1.exe';
procedure TForm1.Button1Click(Sender : TObject);
var path: String;
begin
path := ExtractFilePath(ParamStr(0 ));
ShellExecute(0, 'open', NEWEXE, PChar(ParamStr(0)) , PChar(path), SW_SHOW);
Application.Terminate;
end;
procedure TForm1.FormCreate(Sender: TObject);
var path: String;
begin
if ParamCount > 0 then
begin
Sleep(500); // just to be safe
path := ExtractFilePath(ParamStr(1 ));
DeleteFile(ParamStr(1));
CopyFile(NEWEXE, PChar(ParamStr(1)), false);
ShellExecute(0, 'open', PChar(ParamStr(1)) , '', PChar(path), SW_SHOW);
Application.Terminate;
end;
end;
Uses ShellAPI;
const NEWEXE = 'p:\newexe\project1.exe';
procedure TForm1.Button1Click(Sender
var path: String;
begin
path := ExtractFilePath(ParamStr(0
ShellExecute(0, 'open', NEWEXE, PChar(ParamStr(0)) , PChar(path), SW_SHOW);
Application.Terminate;
end;
procedure TForm1.FormCreate(Sender: TObject);
var path: String;
begin
if ParamCount > 0 then
begin
Sleep(500); // just to be safe
path := ExtractFilePath(ParamStr(1
DeleteFile(ParamStr(1));
CopyFile(NEWEXE, PChar(ParamStr(1)), false);
ShellExecute(0, 'open', PChar(ParamStr(1)) , '', PChar(path), SW_SHOW);
Application.Terminate;
end;
end;
Hi Esk,
I would do it this way:
Make program Loader. I will be permanently active (resident) and will do this:
1. Starts main (supported application) using API CreateProcess.
2. Checks its own mailslot for messages requiring update(they could include additional info, for example the name and the location of new EXE).
3. Terminates supported application sending it request by mailslot, or (brute force) terminates it using API TerminateProcess or ExitProcess. (Process started with CreateProcess can be terminated this way).
4. Copies new EXE file.
5. Starts new EXE file.
6. Waits some time ... and goes to point 2.
No need of any BAT files (by the way I don't know how a BAT file could check if application is still running...)
Jo.
I would do it this way:
Make program Loader. I will be permanently active (resident) and will do this:
1. Starts main (supported application) using API CreateProcess.
2. Checks its own mailslot for messages requiring update(they could include additional info, for example the name and the location of new EXE).
3. Terminates supported application sending it request by mailslot, or (brute force) terminates it using API TerminateProcess or ExitProcess. (Process started with CreateProcess can be terminated this way).
4. Copies new EXE file.
5. Starts new EXE file.
6. Waits some time ... and goes to point 2.
No need of any BAT files (by the way I don't know how a BAT file could check if application is still running...)
Jo.
ASKER
What is this doing?
procedure TForm1.DeleteSelf;
//************************ ********** ********** ********** ***
var
F: TextFile;
batName: string;
pi: TProcessInformation;
si: TStartupInfo;
begin
batName := ExtractFilePath(ParamStr(0 )) + '\Program.bat';
AssignFile(F,batName);
Rewrite(F);
Writeln(F,':try');
Writeln(F,'del "'+ParamStr(0)+'"');
Writeln(F,'if exist "'+ ParamStr(0)+'"'+' goto try');
Writeln(F,'del "' + batName + '"' );
CloseFile(F);
FillChar(si, SizeOf(si), $00);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_HIDE;
if CreateProcess( nil, PChar(batName), nil, nil, False,
IDLE_PRIORITY_CLASS,
nil, nil, si, pi ) then
begin
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
end;
end;
procedure TForm1.DeleteSelf;
//************************
var
F: TextFile;
batName: string;
pi: TProcessInformation;
si: TStartupInfo;
begin
batName := ExtractFilePath(ParamStr(0
AssignFile(F,batName);
Rewrite(F);
Writeln(F,':try');
Writeln(F,'del "'+ParamStr(0)+'"');
Writeln(F,'if exist "'+ ParamStr(0)+'"'+' goto try');
Writeln(F,'del "' + batName + '"' );
CloseFile(F);
FillChar(si, SizeOf(si), $00);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_HIDE;
if CreateProcess( nil, PChar(batName), nil, nil, False,
IDLE_PRIORITY_CLASS,
nil, nil, si, pi ) then
begin
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
end;
end;
ASKER
Epsylon,
const is the other program that i am going to update
when i press button(1) the old app will not delete it self
Esk
const is the other program that i am going to update
when i press button(1) the old app will not delete it self
Esk
Const NEWEXE is the path/filename of the new version of the app.
It's not the old app that deletes itself.
When the client is running an old version and receives an upgrade request (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 client directory and runs the new version. The first instance of the new version terminates leaving the second instance running.
I have tested it and it works.
It's not the old app that deletes itself.
When the client is running an old version and receives an upgrade request (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 client directory and runs the new version. The first instance of the new version terminates leaving the second instance running.
I have tested it and it works.
You can rename your running application, copy the new one from the server and then start it with ParamStr(0)+' 'ParamStr(1)+...
I already used this technic for my one of my own applications.
I already used this technic for my one of my own applications.
>You can rename your running application
Are you sure??? Please verify that...
Are you sure??? Please verify that...
Yes, I'm really shure (but not tested in Win9x environment).
I did, and it does not work (Win98).
ASKER
Epsylon
what does ParamStr(0) mean and paramStr(1)?
Esk
what does ParamStr(0) mean and paramStr(1)?
Esk
ASKER
Epsylon
what does ParamStr(0) mean and paramStr(1)?
Esk
what does ParamStr(0) mean and paramStr(1)?
Esk
From Delphi help:
-------------------------- -------
function ParamStr(Index: Integer): string;
Description
ParamStr returns the parameter from the command line that corresponds to Index, or an empty string if Index is greater than ParamCount. For example, an Index value of 2 returns the second command-line parameter.
ParamStr(0) returns the path and file name of the executing program (for example, C:\TEST\MYPROG.EXE).
-------------------------- -------
Eps.
--------------------------
function ParamStr(Index: Integer): string;
Description
ParamStr returns the parameter from the command line that corresponds to Index, or an empty string if Index is greater than ParamCount. For example, an Index value of 2 returns the second command-line parameter.
ParamStr(0) returns the path and file name of the executing program (for example, C:\TEST\MYPROG.EXE).
--------------------------
Eps.
Instead of ParamStr(0) you can also use:
Application.ExeName
Application.ExeName
Epsylon, you're right! It seems that I have to change my update procedure. I thought about a help program that copies the newer exe from the server (overriding the old version) and start it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think Sleep(500) wont be enough for slow PCs or when CPU usage is above 85%.
Another suggestion how to wait till the target of the copy action can be accessed:
begin
Leaps:=0;
repeat
HFile:=CreateFile(PChar(Ta rget), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
if HFile=INVALID_HANDLE_VALUE then
Sleep(500);
inc(Leaps);
until (HFile<>INVALID_HANDLE_VAL UE) or (Leaps>720);
end;
begin
Leaps:=0;
repeat
HFile:=CreateFile(PChar(Ta
if HFile=INVALID_HANDLE_VALUE
Sleep(500);
inc(Leaps);
until (HFile<>INVALID_HANDLE_VAL
end;
This works too (replacement for the FormCreate method of my answer):
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;
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)
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;
Esk, say something....
i just tested also and seems to works good..
ASKER
This is what i'm looking for...
Thanks
Esk
Thanks
Esk