Delete.exe

I'm trying to write a pretty useless program which appears to delete itself when run. I have a certain idea on how the structure of the program should be, but there are some things I need clearing up on.

Since the executable itself has to disappear the executable can not be running when the executable is deleted. Therefore I must have a second application, however this cannot exist on the hard drive at any time.

Program 1

1. Find path to self. I could make this a default but that would be kinda lame.
The path will be in the string "path". I've written that already.
2. Put Program 2 in memory and cause it to run.
Not sure how to do this. Help is appreciated.
3. Check whether the process of Program 2 is running.
No idea how to do this.
4. Send path to Program 2.
Shouldn't be too hard but I'm not sure what that would require.
5. Return 0
Now this I know how to do.

Program 2

1. Take path from Program 1.
No idea how this is done.
2. Check for process of Program 1 until it is gone.
I can take this from the solution to P1-3
3. When Program 1 is no longer running delete the executable.
No problem.
4. Flush memory.
std::endl should do it.
5. Return 0
Should be fine.

I am of course going to do update this as it goes.

Thank you in advance for all help.
Ok, you could try running program 2 with an argument as the path of program 1. Don't know how you would go about delete/starting stuff without resorting to OS specific programming though.
if your main is declared as int main(int argc, char *argv[]), then argv[0] already has the full name of the program.

The process id of a program in Windows is a sync object, which means you wait on it. If you have the process id of the program, you can use WaitForSingleObject to wait for it to complete, then delete it.

If it just won't terminate, you can move it out of the way on reboot with MoveFileEx, then delete it.
@kbw:
So let's see if I got this right. Program 2 will have to use WaitForSingleObject untill Program 1 exits. Seeing as Program 1 will finish before Program 2 that line won't be needed in Program 1.

The most difficult however will probably getting Program 2 into the memory in a way that it can run without having it on the HDD at any time. Would it be possible to write program 2 first then copy the binary by opening it notepad and copying the content into a string? That would of course depend on program 1 not flushing the memory after it finishes. And how would it then run?
This is a very interesting topic. How would you make windows run a process from a piece of binary data? Is there a function or command that does something along the lines: "load this piece of binary into the memory and register it as a process"? (whatever that means?)
@tition
I'm not sure if there is actually a command for it in C++. Seeing as all binaries run from the memory it shouldn't be so different when bypassing the HDD.
You absolutely don't need another exe.
A self-deleting exe is a Win32 FAQ for about 18 years...
Windows locks running executable files. Are you sure an exe can delete itself? I don't think so. I'd expect to get an Access Denied error.
kick-ass topic you have here... i would like to venture into the project you are speaking of, though, my knowledge of using the windows api is very limited, but, i do know one thing.. when you dynamically allocate memory using the operator NEW it doesn't delete its own self when the program ends without the operator DELETE
@george135:
Really, there is? Could you please elaborate? Ill hit google for a bit and see what I find.
Any update on your progress?
As kbw mentioned, MoveFileEx can be used to make a self-deleting executable. As long as it is acceptable to reboot, it's fairly quick and easy to use this method. Choose NULL as the new file path, and use the MOVEFILE_DELAY_UNTIL_REBOOT flag. Reboot the system and the exe will be gone.
http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx
Topic archived. No new replies allowed.