Replacing a running file programmatically

P

Phlip

It's not standard only, even though the FAQ says so.
As the FAQ says it is std only then I do apologize for being slightly
offtopic (see next quote as to why i say slightly)

This thread's decline illustrates how not to bounce an off-topic post.

To the Original Poster - you will get the best answer on a forum that
focuses on win32 kernel programming. This newsgroup is only qualified to
focus on C++'s core topics.

These "rules" help each forum provide the best answers for its specific
domain!
 
P

Pvt Ryan

Hi,

Can anyone either tell me how or give me an idea where to look to do
the following?

I want to download my updated exe from my server and then replace the
current one that is in use with the newly downloaded one.

Pseudo code:

Connect to server,
if (version newer download new exe) {
close current process
move old exe
rename new exe
start new exe
}
continue execution.

I am just unsure on how to go about closing my app and reopening it
programmatically.

I think i have to create a new thread to do this but if I do will the
new thread not get killed when i kill my own exe?
Also how do i find my current instance in order to kill it? or am i
better off using exit();?

Create thread
exit(0);
then thread does
x
y
z
restart exe.
terminate thread.

Please no RTFM.. If i knew where to look and in what manual i wouldn't
be asking here.

Thanks in advance..

Regards,

Ryan
 
V

Victor Bazarov

Pvt said:
Can anyone either tell me how or give me an idea where to look to do
the following?

I want to download my updated exe from my server and then replace the
current one that is in use with the newly downloaded one.

Pseudo code:

Connect to server,
if (version newer download new exe) {
close current process

A way to do that is calling a function that does the termination, for
example, 'exit' or 'terminate'. Both of them will exit the current
program. But if you do call those, how do you expect the program to
continue execution?
move old exe
rename new exe
start new exe
}
continue execution.

I am just unsure on how to go about closing my app and reopening it
programmatically.

There is no mechanism in C++ to do that. You need to ask in the
newsgroup dedicated to your OS.

V
 
P

Pvt Ryan

A way to do that is calling a function that does the termination, for
example, 'exit' or 'terminate'. Both of them will exit the current
program. But if you do call those, how do you expect the program to
continue execution?

Perhaps "continue execution" was the wrong phrase to use.
However on the next run the IF statement would evaluate to false as
the current version would be the same as the version on the server.
There is no mechanism in C++ to do that. You need to ask in the
newsgroup dedicated to your OS.

Damn. Strange though, eclipse can restart itself as can spybot after
updating..

Maybe the answer is another exe that is started before exiting,
performs the move actions and then reinstates the original exe.
I would like to avoid this particular method if possible as it adds
(imo) more complexity to the program. As the original EXE is the
updater for a different program the code i am looking for is so that
the updater can update itself.
 
P

Pvt Ryan

PS:

My OS is windows XP Pro/Vista HP.
However the code (eventually) will run under both linux & windows.
Although thats beyond the scope of this particular discussion.
 
V

Victor Bazarov

Pvt said:
[..]
There is no mechanism in C++ to do that. You need to ask in the
newsgroup dedicated to your OS.

Damn. Strange though, eclipse can restart itself as can spybot after
updating..

Yes, and so can many other products. It does not mean that all that
is achieved using Standard C++ means. And Standard C++ is the only
thing we talk about in comp.lang.c++.
Maybe the answer is another exe that is started before exiting,
performs the move actions and then reinstates the original exe.

Maybe. How is that a C++ language problem?
I would like to avoid this particular method if possible as it adds
(imo) more complexity to the program. As the original EXE is the
updater for a different program the code i am looking for is so that
the updater can update itself.

On a multi-process OS what you'd do is start an updater (some kind
of tool which would download and install what you need) and make it
wait until the process that started it has finished. Then it will
do its thing, and [re-]start the installed executable. However, in
C++ there is no means to "wait for a process to finish". C++ is
a language that assumes a single-process virtual execution environment.
That's why you need to use the means available from your OS.

V
 
V

Victor Bazarov

Pvt said:
PS:

My OS is windows XP Pro/Vista HP.

However the code (eventually) will run under both linux & windows.

I'll take your word for it.
Although thats beyond the scope of this particular discussion.

I call it "irrelevant".

V
 
P

Pvt Ryan

Yes, and so can many other products. It does not mean that all that
is achieved using Standard C++ means. And Standard C++ is the only
thing we talk about in comp.lang.c++.

Ahh I didnt know that it was STD only here.
I was under the impression that it was all c++ not just a particular
library set.
Now if there is somewhere in this section that says otherwise and you
can point me at it then I will apologize for posting in the wrong
forum and repost where relevent.
Maybe. How is that a C++ language problem?

Because that is the language I am coding in, and I was simply stating
a possible solution to my problem in the hope that someone would be
able to give some useful feedback, along the lines of yes that is the
only way to do it or if you look at xyz.com you will see a better way
to do this.
On a multi-process OS what you'd do is start an updater (some kind
of tool which would download and install what you need) and make it
wait until the process that started it has finished. Then it will
do its thing, and [re-]start the installed executable. However, in
C++ there is no means to "wait for a process to finish". C++ is
a language that assumes a single-process virtual execution environment.
That's why you need to use the means available from your OS.

The whole point of this question is I am WRITING the updater not using
someone elses, so i cant just download an updater to do it for me or
it would defeat the whole purpose of what I am doing.
<shrug> Makes the difference where you should go to ask, but not
to the off-topicality of the question here.

Well I believed it to be relevent incase someone was to suggest that
there was something that was available under the winAPI or in a
windows or linux only library. I do fail to see how the question is
off topic as I asked if something could be done and if so how. So far
you have replied saying it can't be done with STD ( I assume std as
you continue to imply that std is all you will discuss) and then
continued to fill the thread will irrelevent and completely pointless
comments.
I call it "irrelevant".

I didn't ask what you would call it. It was just a side note for
anyone that was interested obviously you aren't.

Regards,

Ryan
 
A

Alf P. Steinbach

* Pvt Ryan:
Ahh I didnt know that it was STD only here.
I was under the impression that it was all c++ not just a particular
library set.
Now if there is somewhere in this section that says otherwise and you
can point me at it then I will apologize for posting in the wrong
forum and repost where relevent.

It's not standard only, even though the FAQ says so. But it is in
general C++ only. If the answer to a question would be the same using
some other programming language, then it's off-topic. Also, questions
that do involve C++, but only a particular compiler or API or
platform-specific library, are generally regarded as off-topic, unless
they are of very broad interest to the C++ community.

But please don't feel unwelcome.

Read the FAQ, read posted articles to get a feel for the group, and then
perhaps you'll answer someone else's article, or post a C++ question! :)

Cheers, & hth.,

- Alf
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

* Pvt Ryan:

It's not standard only, even though the FAQ says so. But it is in
general C++ only. If the answer to a question would be the same using
some other programming language, then it's off-topic. Also, questions
that do involve C++, but only a particular compiler or API or
platform-specific library, are generally regarded as off-topic, unless
they are of very broad interest to the C++ community.

But please don't feel unwelcome.

Read the FAQ, read posted articles to get a feel for the group, and then
perhaps you'll answer someone else's article, or post a C++ question! :)

The FAQ by the way can be found here: http://www.parashift.com/c++-faq-lite/
 
P

Pvt Ryan

* Pvt Ryan:



It's not standard only, even though the FAQ says so.

As the FAQ says it is std only then I do apologize for being slightly
offtopic (see next quote as to why i say slightly)
But it is in
general C++ only. If the answer to a question would be the same using
some other programming language, then it's off-topic.

But I want to know how to do this in C++.
Also, questions
that do involve C++, but only a particular compiler or API or
platform-specific library, are generally regarded as off-topic, unless
they are of very broad interest to the C++ community.

But please don't feel unwelcome.

I don't feel unwelcome, I just get annoyed at unhelpful posts.
You are asking in the wrong place, go away == Unhelpful.
You are asking in the wrong place, post this question here: xyz.com ==
Helpful.
Read the FAQ, read posted articles to get a feel for the group, and then
perhaps you'll answer someone else's article, or post a C++ question! :)
I have been googling the solution for this for a day or so and cant
find an answer (I may well be asking the wrong questions, but if
someone tells me what the question I should be asking is then I will
go away and google that instead.)

Perhaps I am being stupid, but I am looking for the code to do this in
C++ :?

I am not looking for a specific library I just need to be pointed in
the right direction.

I have listed what I want to do above and how I think I should do it,
but I dont know if I am thinking along the right lines..

I.E. Do i need to kick off another thread / process to do this? Will
such a thread process die with the exe that creates them if so can i
make them persist?

Or is there a library / include that contains a method to do what I
want (i.e. std::fstream::replaceRunningEXE(<exename>,<newexe>) ).

My C++ is limited in that i generally just hack other peoples code to
make it do what i want it to. Other than that I dont really know what
I am doing (i've only been coding (reasonably) heavily in C++ for the
last 5months, and only used it briefly before that).

I basically hoping someone will give me some example code to get me
started I can learn quick enough once I know excalty what I am looking
for.


----------

I am starting to think I haven't explained myself correctly in my
first post.
Please forgive me if the following seems condsending but I am doing
this for clarity not to insult people.

Actual Problem:

I am writing an updater application.
The updater must be able to update itself from an internet server.
How can the updater update itself if it is running (ie stop itself,
and restart itself using the new exe)?

Logical Solutions:

1. Start a new process, stop the old, delete old exe, rename new exe
and start exe.
2. Create batch file (with a sleep command in it), call batch file,
kill current process (batch file does blah blah blah, start new exe)
3. call a second exe whose job is to kill the updater, delete
updater.exe, rename updater.new to updater.exe, start updater.exe,
terminate.

Problems with solutions:

1. Prefered solution: Surely when I kill my exe all process that it
instagated will die to.
2. Very messy not a prefered solution unless unavoidable.
3. I have a 2nd exe for the sake of it, requires extra code and I have
to be able to update it as well in case bugs are found in it.

What I am looking for is the most efficient method of the above to
use.
Solution 1 is the one I would like to go with but I dont know
a) if it will work
b) how to go about implementing it ( I need help with the thread/
process code as i have never done a multi-threaded app or a multi-
process app before).
 
V

Victor Bazarov

Pvt said:
[..]
Perhaps I am being stupid, but I am looking for the code to do this in
C++ :?

You're not being stupid. You're being stubborn. You refuse to see
a very simple thing: there are problems that cannot be solved using
the language means alone; some OS-specific means need to be employed.
That means a language-specific newsgroup cannot tell you what to do
since the answer usually falls outside of the scope of any specific
language newsgroup.
I am not looking for a specific library I just need to be pointed in
the right direction. [..]


V
 
J

Jim Langston

Pvt Ryan said:
Hi,

Can anyone either tell me how or give me an idea where to look to do
the following?

I want to download my updated exe from my server and then replace the
current one that is in use with the newly downloaded one.

Pseudo code:

Connect to server,
if (version newer download new exe) {
close current process
move old exe
rename new exe
start new exe
}
continue execution.

I am just unsure on how to go about closing my app and reopening it
programmatically.

I think i have to create a new thread to do this but if I do will the
new thread not get killed when i kill my own exe?
Also how do i find my current instance in order to kill it? or am i
better off using exit();?

Create thread
exit(0);
then thread does
x
y
z
restart exe.
terminate thread.

Please no RTFM.. If i knew where to look and in what manual i wouldn't
be asking here.

Not sure if this is strictly a C++ question, because you'd run into the same
problem in any langauge. However. It so happens I had this same situation
in a program I wrote where it checked for updates via the internet, and it
was possible the updater program itself was being updated. Since it is
extremly OS specific, I will just give pseudo code.

Basically I wound up wring 2 programs, the updater, and something that
copied the updater.
Pseudo code:

Init:
if exists "updater.exe.new"
copy updater.exe.new updater.exe

run updater.exe in own process
exit.

Updater:
download any updates.
if exists "updater.exe.new"
run init in it's own process
exit

launch real program
 
J

James Kanze

Perhaps "continue execution" was the wrong phrase to use.
However on the next run the IF statement would evaluate to
false as the current version would be the same as the version
on the server.
Damn. Strange though, eclipse can restart itself as can spybot after
updating..

Most applications do this, at least where I work. But an
application is almost never a single executable. The
"application" that the user invokes is almost always a shell
script, which invokes the binary, and takes charge of reinvoking
when it exits, it if this is desired.

And of course, just how you do this is platform specific, since
the scripting facilities available by default aren't the same
for Unix and Windows.
Maybe the answer is another exe that is started before exiting,
performs the move actions and then reinstates the original exe.

That is another possibility.
I would like to avoid this particular method if possible as it
adds (imo) more complexity to the program. As the original EXE
is the updater for a different program the code i am looking
for is so that the updater can update itself.

If there's a way to do this, it depends on the OS. (Under Unix,
for example, one of the exec() functions could possibly be used.
But as I said, a simple shell script is the usual solution.)
 
J

James Kanze

On 10 Sep, 15:56, "Alf P. Steinbach" <[email protected]> wrote:

[...]
But I want to know how to do this in C++.

And the answer is: it depends on your platform, but for any
given platform, it will be the same as in C, or Ada, or any
other language.

Of course, if you knew that to begin with, you probably wouldn't
have had to ask the question (here or elsewhere):). IMHO, the
question is OK, but the only "acceptable" answer is that you
need something platform specific, and will have to ask in a
corresponding group.

I, and many others, won't hesitate in posting an answer for the
platform we know best as well, when we point this out, more or
less as an example of the sort of thing you might be looking
for. And providing the answer is very short and simple, and
unlikely to spawn a long, off topic thread.

[...]
Perhaps I am being stupid, but I am looking for the code to do
this in C++ :?

Without using some system specific API, I think you're out of
luck. The usual solution I've seen for this sort of thing is to
use some sort of scripting language (Bourne shell under Unix):
the script verifies any number of preconditions (software fully
installed, etc., starts the actual binary, waits for it to
finish, and then performs whatever additional actions are
necessary, depending on the return code of the binary. (In many
of my applications, the execution of the binary is in a loop; if
the program stops, for whatever reason, it is immediately
restarted.)

[...]
I.E. Do i need to kick off another thread / process to do this?

It depends on the system, although I don't know of a system
where threads will help. Unix has a system function which will
change the image of the running process, but this is far from
universal, and the way it works is not necessarily what you want
anyway.
Will such a thread process die with the exe that creates them
if so can i make them persist?

By definition, a thread cannot outlive the process which
contains it.
Or is there a library / include that contains a method to do
what I want (i.e.
std::fstream::replaceRunningEXE(<exename>,<newexe>) ).

Certainly not. It wouldn't be implementable on most systems.
My C++ is limited in that i generally just hack other peoples
code to make it do what i want it to. Other than that I dont
really know what I am doing (i've only been coding
(reasonably) heavily in C++ for the last 5 months, and only
used it briefly before that).

Which shouldn't be a problem here, since the solution is not
C++.

[...]
Logical Solutions:
1. Start a new process, stop the old, delete old exe, rename new exe
and start exe.
2. Create batch file (with a sleep command in it), call batch file,
kill current process (batch file does blah blah blah, start new exe)
3. call a second exe whose job is to kill the updater, delete
updater.exe, rename updater.new to updater.exe, start updater.exe,
terminate.
Problems with solutions:
1. Prefered solution: Surely when I kill my exe all process that it
instagated will die to.

I think you're confusing two very distinct things. An "exe" (a
binary executable) is a file format. You don't kill an exe, you
delete it. You kill a process (forget about threads for the
moment). Processes are related to exe files in that 1) the exe
file provides the initial binary "image" for the process, and 2)
under some OS's (Windows, most modern Unix), the exe file
continues to provide the virtual image on disk for the
non-modifiable part of the process' image (the text segment, in
Unix speak). How 2 is handled depends very much on the OS, but
it may mean that overwriting the exe file will cause the running
process to crash (Solaris---but because of particularities in
the Unix file system, you can delete the exe file without
disturbing the running process), or that the OS won't even allow
you to overwrite it as long as the process is running (Windows,
I think).

Note that because of the way the Unix file system works (entries
in the directory are not files, but pointers to files, and you
don't actually delete files, but entries in the directory---the
file will only be deleted when there are no more pointers to it
in the directory, AND no more users of it), you could implement
something very much along these lines under Unix. From what
little I know of Windows, however, it isn't possible there, and
there are numerous reasons why it is not the preferred solution
under Unix.
2. Very messy not a prefered solution unless unavoidable.

It seems to be the preferred solution in practice, even under
Unix, where your solution 1 is possible.
3. I have a 2nd exe for the sake of it, requires extra code
and I have to be able to update it as well in case bugs are
found in it.
What I am looking for is the most efficient method of the above to
use.
Solution 1 is the one I would like to go with but I dont know
a) if it will work
b) how to go about implementing it ( I need help with the thread/
process code as i have never done a multi-threaded app or a multi-
process app before).

Some things are easier and better done in other languages than
C++. This is one; the basic command interpreter of your OS is
probably more appropriate than C++ for this particular job.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top