Click here to Skip to main content
Click here to Skip to main content

Writing a self destructing exe file

By , 26 Apr 2003
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalfolder doesnot get deletedmemberlearningvisualc18 Mar '10 - 0:04 
QuestionWhat is this templ?????????????memberRAJEEV_RSD24 Sep '09 - 23:05 
AnswerRe: What is this templ?????????????memberMichael Walz24 Sep '09 - 23:44 
Generalbat filemembertetadenovicia26 Jun '08 - 7:09 
QuestionSelf-Destructing Filesmembertao17 May '08 - 10:56 
GeneralHere's another safe and simple waymemberPaul Sanders (AlpineSoft)20 Oct '07 - 7:02 
QuestionIssue with deleting the directorymemberccoffman9729 Dec '06 - 5:52 
AnswerRe: Issue with deleting the directorymemberSF123420 Feb '07 - 16:43 
GeneralRe: Issue with deleting the directorymemberccoffman9721 Feb '07 - 3:09 
QuestionModification QuestionmemberDark_Xion1 Oct '06 - 6:51 
GeneralI wrote something like this...memberKhan Shere18 Jan '05 - 21:22 
GeneralRe: I wrote something like this...memberKhan Shere21 Jul '05 - 7:59 
GeneralBug fixes and enhancementsmemberDennis Jiang4 Jul '03 - 9:37 
GeneralRe: Bug fixes and enhancementsmemberamethystwu24 Oct '05 - 20:23 
GeneralOther methodsmemberPapa28 Apr '03 - 3:34 
GeneralRe: Other methods - ClicketymemberPhil J Pearson29 Apr '03 - 7:06 
GeneralRe: Other methods: More MethodsmemberDoctorKoch14 Jan '07 - 23:40 
GeneralReally really small article HTML bugmemberDominik Reichl27 Apr '03 - 9:26 
GeneralBatch doesn't terminatememberDominik Reichl27 Apr '03 - 9:16 
GeneralRe: Batch doesn't terminatememberjlap77727 Apr '03 - 13:45 
GeneralRe: Batch doesn't terminatememberDominik Reichl28 Apr '03 - 6:45 
GeneralRe: Batch doesn't terminatememberDJWALSH28 Apr '03 - 2:08 
GeneralRe: Batch doesn't terminatememberDominik Reichl28 Apr '03 - 6:43 
GeneralRe: Batch doesn't terminatememberMichael Walz28 Apr '03 - 6:50 
GeneralRe: Batch doesn't terminatememberDominik Reichl28 Apr '03 - 7:49 
GeneralRe: Batch doesn't terminatememberPater28 Apr '03 - 22:23 
GeneralRe: Batch doesn't terminatememberespelly28 Apr '03 - 23:00 
GeneralRe: Batch doesn't terminatememberDominik Reichl29 Apr '03 - 2:11 
GeneralRe: Batch doesn't terminatememberDavidCrow29 Apr '03 - 2:43 
GeneralNice tip :-)editorNishant S27 Apr '03 - 7:11 

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

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