In a Dev-C++ project, I want to run an .exe file (simple.exe in TEMP folder) and delete the file (after the project is terminated) when simple.exe is closed.
Is it possible managing this while the project is not running.
doh, sorry - I meant "simply have your executable create and run that batch file and the .bat file will continue running"
(didnt have my Red Bull this morning)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-20
A process cannot delete itself after it has terminated (because it is not running!). However a process can delete a file of course, so you can have one process run another and then delete it when it terminates - i.e. you need two executables.
However, why would you want to do such a thing? It is trivial so you should not need any help, but you won't get much support for writing your suspiciously behaving code here!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have an password protected .exe file which has two parts. First part of it is an extractor .exe stub file and the second part is an encrypted .exe file.
If the user enter correct password, stub file decrypts the second part of the file, save it to a temporary folder and run it.
Since the stub file is terminated while decrypted .exe is running, I can not delete it. I am looking for an alternative way of managing this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-20
Why terminate the stub file? Get it to wait until the created process terminates then delete the file.
If you launch the decrypted .exe using one of the spawn functions with mode = _P_WAIT ( http://msdn2.microsoft.com/en-us/library/20y988d2(VS.71).aspx ), the spawn function does not return until the spawned process terminates.
A slightly more convoluted method if you don't want the starter process hanging around is to create a third executable that repeatedly attempts to delete the temporary executable until it succeeds, and have this launched by the temporary itself just before it terminates.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am going to try to decrypt the file to memory (hard work). On failure, the most preferable method is not to terminate the stub file and using a spawn function even if it is not a practical way for the project.
Thank you very much for your kind help!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-21
If you are going to decrypt directly to memory, you will have to replace the functionality of the OS loader, which is no doubt not as simple as reading the file to memory and jumping to the start location. It may also look like at attempt to execute a data segment - an action that indicates either buggy or malicious code. All recent Intel processors and Microsoft OSs (Win2k or later) have have hardware/software protection mechanisms to prevent a process from executing code in a data segment.
There are ways of making the starter stub hide itself so that there is no visible window while it waits for the decrypted process to run. If this is a GUI app. it is especially simple.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
.exe's can't delete themselves while they're running .... but .bat's can.
It's not the most elegant solution but it works ... example -
:DeleteFile
del c:\myexe.exe
if exist c:\myexe.exe goto DeleteFile
del c:\delmyexe.bat
exit
--
Simply have your .bat file create and run that executable and the .bat will continue running in a loop until c:\myexe.exe has been deleted, at which stage it will delete the .bat file also.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-29
... or have your executable create and run the batch file.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In a Dev-C++ project, I want to run an .exe file (simple.exe in TEMP folder) and delete the file (after the project is terminated) when simple.exe is closed.
Is it possible managing this while the project is not running.
char TempFileName[MAX_PATH + 1];
TempFileName[MAX_PATH] = '\0';
GetTempPath(MAX_PATH, TempFileName);
strcat (TempFileName, "simple.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
CreateProcess(NULL, TempFileName, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
// Here the main project .exe is terminated
doh, sorry - I meant "simply have your executable create and run that batch file and the .bat file will continue running"
(didnt have my Red Bull this morning)
A process cannot delete itself after it has terminated (because it is not running!). However a process can delete a file of course, so you can have one process run another and then delete it when it terminates - i.e. you need two executables.
However, why would you want to do such a thing? It is trivial so you should not need any help, but you won't get much support for writing your suspiciously behaving code here!
I am afraid I couldn't express myself truly.
I have an password protected .exe file which has two parts. First part of it is an extractor .exe stub file and the second part is an encrypted .exe file.
If the user enter correct password, stub file decrypts the second part of the file, save it to a temporary folder and run it.
Since the stub file is terminated while decrypted .exe is running, I can not delete it. I am looking for an alternative way of managing this.
Why terminate the stub file? Get it to wait until the created process terminates then delete the file.
If you launch the decrypted .exe using one of the spawn functions with mode = _P_WAIT ( http://msdn2.microsoft.com/en-us/library/20y988d2(VS.71).aspx ), the spawn function does not return until the spawned process terminates.
A slightly more convoluted method if you don't want the starter process hanging around is to create a third executable that repeatedly attempts to delete the temporary executable until it succeeds, and have this launched by the temporary itself just before it terminates.
Clifford
I am going to try to decrypt the file to memory (hard work). On failure, the most preferable method is not to terminate the stub file and using a spawn function even if it is not a practical way for the project.
Thank you very much for your kind help!
If you are going to decrypt directly to memory, you will have to replace the functionality of the OS loader, which is no doubt not as simple as reading the file to memory and jumping to the start location. It may also look like at attempt to execute a data segment - an action that indicates either buggy or malicious code. All recent Intel processors and Microsoft OSs (Win2k or later) have have hardware/software protection mechanisms to prevent a process from executing code in a data segment.
There are ways of making the starter stub hide itself so that there is no visible window while it waits for the decrypted process to run. If this is a GUI app. it is especially simple.
Clifford
You may need this is you are going to attempt to build your own program loader: http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
It is beyond doubt that I need this for the project. Thanks again!
.exe's can't delete themselves while they're running .... but .bat's can.
It's not the most elegant solution but it works ... example -
:DeleteFile
del c:\myexe.exe
if exist c:\myexe.exe goto DeleteFile
del c:\delmyexe.bat
exit
--
Simply have your .bat file create and run that executable and the .bat will continue running in a loop until c:\myexe.exe has been deleted, at which stage it will delete the .bat file also.
... or have your executable create and run the batch file.