ghell
12-01-2007, 06:56 PM
Is it possible to get an executable to write to itself? The application I am writing needs to check for updates and if there are any, update itself.
Here is an example of what I want to do:#include <stdio.h>
int main(int argc, char* argv[])
{
/* Open self for writing */
FILE* f = fopen(argv[0], "w");
if(f == NULL)
{
fprintf(stderr, "Unable to open file %s.", argv[0]);
return 1;
}
fclose(f);
return 0;
}
I actually want to do it from C# on Windows but it was easier to demonstrate the problem in C.
What I am currently doing is downloading to a temporary file, getting the program to run another executable and quit. The other executable then moves the temporary file over the first executable while it is not in use. However, this isn't very elegant and I have seen updater software before that did not exhibit this behaviour.
Here is an example of what I want to do:#include <stdio.h>
int main(int argc, char* argv[])
{
/* Open self for writing */
FILE* f = fopen(argv[0], "w");
if(f == NULL)
{
fprintf(stderr, "Unable to open file %s.", argv[0]);
return 1;
}
fclose(f);
return 0;
}
I actually want to do it from C# on Windows but it was easier to demonstrate the problem in C.
What I am currently doing is downloading to a temporary file, getting the program to run another executable and quit. The other executable then moves the temporary file over the first executable while it is not in use. However, this isn't very elegant and I have seen updater software before that did not exhibit this behaviour.