Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / MFC
Article

Writing a self destructing exe file

Rate me:
Please Sign up or sign in to vote.
4.23/5 (29 votes)
26 Apr 2003Public Domain 188.3K   61   30
Explains how you can have a program delete itself once it has finished running without a reboot

Introduction

Uninstall programs typically want to delete themselves at the end of the un-installation, but executable cannot delete itself by simply calling the DeleteFile function. By calling the Selfdestruct() function showed below before program exit, the calling executable will be destroyed as soon as possible. The method shown in this article works on all Win32 platforms and there is no need to reboot.

Using the code

Just call the Selfdestruct() function before program exit.

// this is the name of the temporary .bat file
static const char tempbatname[] = "_uninsep.bat" ;

void Selfdestruct() 
{
  // temporary .bat file
  static char templ[] = 
    ":Repeat\r\n"
    "del \"%s\"\r\n"
    "if exist \"%s\" goto Repeat\r\n"
    "rmdir \"%s\"\r\n"
    "del \"%s\"" ;


  char modulename[_MAX_PATH] ;    // absolute path of calling .exe file
  char temppath[_MAX_PATH] ;      // absolute path of temporary .bat file
  char folder[_MAX_PATH] ;

  GetTempPath(_MAX_PATH, temppath) ;
  strcat(temppath, tempbatname) ;

  GetModuleFileName(NULL, modulename, MAX_PATH) ;
  strcpy (folder, modulename) ;
  char *pb = strrchr(folder, '\\');
  if (pb != NULL)
    *pb = 0 ;

  HANDLE hf ;
  
  hf = CreateFile(temppath, GENERIC_WRITE, 0, NULL, 
              CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
  
  if (hf != INVALID_HANDLE_VALUE)
  {
    DWORD len ;
    char *bat ;

    bat = (char*)alloca(strlen(templ) + 
               strlen(modulename) * 2 + strlen(temppath) + 20) ;

    wsprintf(bat, templ, modulename, modulename, folder, temppath) ;

    WriteFile(hf, bat, strlen(bat), &len, NULL) ;
    CloseHandle(hf) ;

    ShellExecute(NULL, "open", temppath, NULL, NULL, SW_HIDE);
  }
}

How it works

Let's assume the executable that wants to destroy itself is located in c:\myfolder\selfdestruct.exe. The Selfdestruct() function will create following .bat in the computers temp folder and then launches it:

:Repeat
del "c:\myfolder\selfdestruct.exe"
if exist "c:\myfolder\selfdestruct.exe" goto Repeat
rmdir "c:\myfolder"
del "c:\temp\_uninsep.bat" ;

The .bat file will try to delete the c:\myfolder\selfdestruct.exe over and over until it finally succeeds (that is as soon as selfdestruct.exe has finished execution. Then it tries to remove the containing folder (here c:\myfolder) which will work only if it is empty and finally deletes itself. Fortunately .bat files can delete themselves.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl28-Apr-03 6:45
Dominik Reichl28-Apr-03 6:45 
GeneralRe: Batch doesn't terminate Pin
DJWALSH28-Apr-03 2:08
DJWALSH28-Apr-03 2:08 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl28-Apr-03 6:43
Dominik Reichl28-Apr-03 6:43 
GeneralRe: Batch doesn't terminate Pin
Michael Walz28-Apr-03 6:50
Michael Walz28-Apr-03 6:50 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl28-Apr-03 7:49
Dominik Reichl28-Apr-03 7:49 
GeneralRe: Batch doesn't terminate Pin
Pater28-Apr-03 22:23
Pater28-Apr-03 22:23 
GeneralRe: Batch doesn't terminate Pin
espelly28-Apr-03 23:00
espelly28-Apr-03 23:00 
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl29-Apr-03 2:11
Dominik Reichl29-Apr-03 2:11 
espelly wrote:
Try adding "Exit" on the last line off the .bat

I have tried this. The "exit" will never be executed because the batch file has been deleted before! The system shows "Batch file missing" and then, in a second line, "File not found" (on my Win98).
It seems the system opens and closes the file each line.

-Dominik



_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? Wink | ;)

GeneralRe: Batch doesn't terminate Pin
David Crow29-Apr-03 2:43
David Crow29-Apr-03 2:43 
GeneralNice tip :-) Pin
Nish Nishant27-Apr-03 7:11
sitebuilderNish Nishant27-Apr-03 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.