Click here to Skip to main content
Licence Public Domain
First Posted 26 Apr 2003
Views 115,073
Bookmarked 55 times

Writing a self destructing exe file

By | 26 Apr 2003 | Article
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

About the Author

Michael Walz



Switzerland Switzerland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalfolder doesnot get deleted Pinmemberlearningvisualc0:04 18 Mar '10  
QuestionWhat is this templ????????????? PinmemberRAJEEV_RSD23:05 24 Sep '09  
AnswerRe: What is this templ????????????? PinmemberMichael Walz23:44 24 Sep '09  
Generalbat file Pinmembertetadenovicia7:09 26 Jun '08  
QuestionSelf-Destructing Files Pinmembertao110:56 7 May '08  
We Are Developing A Charity Site
For Film / TV / VIDEO / MUSIC / RADIO
We Are Very Much In Need Of A Simple Way
To Create An MP3 File That Will Erase Itself After One Play
Thus A Form Of Executable File That Runs A Small Player
To Play A Song Once And Then Self Destruct
This Is To Help Musicians And Producers
Who Create Work Based On Promises To Pay
And Who Send Demo MP3s To Prove Completion Of Work
And Then The Demo Is Taken Off And Used
And The Artist Is Never Reimbursed
 
The Site Is
 
www.t-film.org
 
In Early Phases Of Development
 
Manifest Communication
 
Manifest Respect
Manifest Peace
Manifest Love
 
Vector Outbound
GeneralHere's another safe and simple way PinmemberPaul Sanders (AlpineSoft)7:02 20 Oct '07  
QuestionIssue with deleting the directory Pinmemberccoffman975:52 29 Dec '06  
AnswerRe: Issue with deleting the directory PinmemberSF123416:43 20 Feb '07  
GeneralRe: Issue with deleting the directory Pinmemberccoffman973:09 21 Feb '07  
QuestionModification Question PinmemberDark_Xion6:51 1 Oct '06  
GeneralI wrote something like this... PinmemberKhan Shere21:22 18 Jan '05  
GeneralRe: I wrote something like this... PinmemberKhan Shere7:59 21 Jul '05  
GeneralBug fixes and enhancements PinmemberDennis Jiang9:37 4 Jul '03  
GeneralRe: Bug fixes and enhancements Pinmemberamethystwu20:23 24 Oct '05  
GeneralOther methods PinmemberPapa3:34 28 Apr '03  
GeneralRe: Other methods - Clickety PinmemberPhil J Pearson7:06 29 Apr '03  
GeneralRe: Other methods: More Methods PinmemberDoctorKoch23:40 14 Jan '07  
GeneralReally really small article HTML bug PinmemberDominik Reichl9:26 27 Apr '03  
GeneralBatch doesn't terminate PinmemberDominik Reichl9:16 27 Apr '03  
GeneralRe: Batch doesn't terminate Pinmemberjlap77713:45 27 Apr '03  
GeneralRe: Batch doesn't terminate PinmemberDominik Reichl6:45 28 Apr '03  
GeneralRe: Batch doesn't terminate PinmemberDJWALSH2:08 28 Apr '03  
GeneralRe: Batch doesn't terminate PinmemberDominik Reichl6:43 28 Apr '03  
GeneralRe: Batch doesn't terminate PinmemberMichael Walz6:50 28 Apr '03  
GeneralRe: Batch doesn't terminate PinmemberDominik Reichl7:49 28 Apr '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 27 Apr 2003
Article Copyright 2003 by Michael Walz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid