tags:

views:

342

answers:

7

How to write a program in C++ such that it will delete itself after execution ?

A: 

It's a legitimate enough question but it seems you don't understand how executables work. Execution places the program in memory so deleting the disk file is trivial (provided you don't also delete run-time dependencies).

SpliFF
If it is executed in memory, why can't I (in windows) delete the executable if the program is running?
devoured elysium
Execution places **parts** of the program in memory - many pages may be left on disk to be loaded later , which is why most OSs don't allow you to delete the file representing an executing process.
anon
Ah, I see. That was mainly what I thought would happen.
devoured elysium
-1 "so deleting the disk file is trivial" This is incorrect. Program can easily delete itself on linux (if it has enough privileges), but on windows file of a currently running program cannot be deleted until program is terminated.
SigTerm
Windows (at least, and probably others too) doesn't copy the contents of the executable file into memory, it *maps* the file into memory, and pages are loaded on demand as they're needed.
Will Dean
The situation in unix is also not a simple as *"places the program in memory"* either as mapping may be used, but the entry in the filesystem *may* be deleted while the file is running without adversely effecting the operation of the program. Thus in unix the only requirement is that the program know where it's own executable is on disk, which is usually but not always `argv[0]`.
dmckee
A: 

You can use

std::remove("file.extension")

to delete specific files. If you need to search for the files you're going to delete you would need some 3rd party library such as http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/index.htm

fogedi
Does that work with a running executable?
devoured elysium
I guess i misunderstood the question but kayrick, and gtikok elaborate further on that issue
fogedi
+4  A: 

Some Methods

You could also, use some kind of Scheduled Task...

st0le
great article, "Solution for XP+" is really awesome
Andrey
+1  A: 

On Unix, just call unlink(2) on the executable.

On Windows, you need a second process to help you. The response from st0le seems to be for unlinking a DLL, but for an executable, you would need to start a second process or use an existing process, and then terminate your executable and have the second process do the deletion.

A very simple approach would be to use cmd.exe to help.

An speculative approach that uses any other process could be to allocate some memory in another process and put the filename you want to delete there, then use CreateRemoteThread() to create a suspended thread in the remote process with an entry point of DeleteFile with an argument of a pointer to the memory you allocated. Then exit your process, the thread suspend count should decrement and then DeleteFile should be called to delete your file.

Issues: Memory leak in the remote process, messy.

An easier way might just be to have a supporting DLL using the techniques from st0le's answer.

janm
A: 

However there is a way to delete the file by itself after execution because every Uninstaller unistalls the software which was installed by it & also deletes the last remaining file i.e., itself such that no files will be remaining in our Hard Disk except some Registry Entries irrespective of the platform it has been installed.

Santosh G Bhat - MIT 091001005
A: 

std::remove(argv[0]) before return in main can do it.

You can't actually be 100% sure that `argv[0]` contains a proper path to the executable. It is supposed to, but there is no enforcement and there are cases in which this is broken for one reason or another.
dmckee
+1  A: 

Here is the code in C which will delete the executable after execution.

#include <Windows.h>
#include <strsafe.h>

#define SELF_REMOVE_STRING  TEXT("cmd.exe /C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"%s\"")

void DelMe()
{
    TCHAR szModuleName[MAX_PATH];
    TCHAR szCmd[2 * MAX_PATH];
    STARTUPINFO si = {0};
    PROCESS_INFORMATION pi = {0};

    GetModuleFileName(NULL, szModuleName, MAX_PATH);

    StringCbPrintf(szCmd, 2 * MAX_PATH, SELF_REMOVE_STRING, szModuleName);

    CreateProcess(NULL, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}

void main()
{
    /* Do what you need */

    /* Call this function at the very end of your program to delete itself */
    DelMe();
}
gtikok