Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Self-deleting 1

Status
Not open for further replies.

imagenetics

Programmer
Dec 8, 2003
66
PL
How to write a program that deletes itself at system startup/shutdown?
 
You cannot delete a program that is running.
What you can do is to create another program that his only functionn consists in delete the programa that you want to delete

--- LastCyborg ---
 
Yes, I know. But what about those installers? You go to a directory, run "uninstall.exe" and all files INCLUDING "uninstall.exe" are deleted.
 
I've seen this done but I don't know how efficient this is. You can write your own unintall program. It creates a program or script (batch file) in the temp directory. This new program starts the minute the calling/creating program stops and it deletes your programs.


James P. Cottingham
[sup]
There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
[/sup]
 
There's a difference between a process an a file. A process can remove a file, even if that file happens to be the image the process is based off of; it just doesn't care.

So...

Code:
// self-remover.c
// Destined to be:
//   C:\self-remover.exe

#include <stdio.h>

int main( void )
{
    int i;

    /* remove the file */
    remove( "C:\\self-remover.exe" );

    /* Do some other stuff */
    printf( "Hello, world.\n" );

    for ( i = 0; i < 3000; ++i )
    {
        printf( "%d\n", i );
    }

    printf( "The file that generated me is gone, "
            "but I, the process, still remain!\n" );

    return 0;
}

would work just fine.
 
Oh, does Windows do something to stop that from happening? Sorry. Ignore me if I'm obviously wrong; I'm a *nix person.
 
I would think you could write a simple bit of executable code that's only job is to delete another set of files when you're done with them, copy that executable into memory and run it from there. This is what some viruses do.

Chris
 
I'we once made a program that after a trial period changed "itself" i.e. the file. It worked like a charm.

Totte
 
You can not delete .exe file if that app is running, because Windows is using Memory-mapped files when executing apps, which means that .exe file is always open and never copied into RAM completly.
 
I've done this before (not in builder, but it should work pretty much the same way), take a look at:


Also, another way that I have done it is creating a DOS app (using one of the old Borland C++ compilers). A DOS exe, even running under win32 OS's, can delete itself.

Have Fun.
 
use API function:
BOOL MoveFileEx(
LPCTSTR lpExistingFileName,
LPCTSTR lpNewFileName,
DWORD dwFlags
);

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top