Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWhat is this templ????????????? Pin
RAJEEV_RSD
0:05 25 Sep '09  
GeneralRe: What is this templ????????????? Pin
Michael Walz
0:44 25 Sep '09  
Generalbat file Pin
tetadenovicia
8:09 26 Jun '08  
QuestionSelf-Destructing Files Pin
tao1
11:56 7 May '08  
GeneralHere's another safe and simple way Pin
Paul Sanders (AlpineSoft)
8:02 20 Oct '07  
QuestionIssue with deleting the directory Pin
ccoffman97
6:52 29 Dec '06  
AnswerRe: Issue with deleting the directory Pin
SF1234
17:43 20 Feb '07  
GeneralRe: Issue with deleting the directory Pin
ccoffman97
4:09 21 Feb '07  
QuestionModification Question Pin
Dark_Xion
7:51 1 Oct '06  
GeneralI wrote something like this... Pin
Khan Shere
22:22 18 Jan '05  
GeneralRe: I wrote something like this... Pin
Khan Shere
8:59 21 Jul '05  
GeneralBug fixes and enhancements Pin
Dennis Jiang
10:37 4 Jul '03  
GeneralRe: Bug fixes and enhancements Pin
amethystwu
21:23 24 Oct '05  
GeneralOther methods Pin
Papa
4:34 28 Apr '03  
GeneralRe: Other methods - Clickety Pin
Phil J Pearson
8:06 29 Apr '03  
GeneralRe: Other methods: More Methods Pin
DoctorKoch
0:40 15 Jan '07  
GeneralReally really small article HTML bug Pin
Dominik Reichl
10:26 27 Apr '03  
GeneralBatch doesn't terminate Pin
Dominik Reichl
10:16 27 Apr '03  
GeneralRe: Batch doesn't terminate Pin
jlap777
14:45 27 Apr '03  
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl
7:45 28 Apr '03  
GeneralRe: Batch doesn't terminate Pin
DJWALSH
3:08 28 Apr '03  
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl
7:43 28 Apr '03  
GeneralRe: Batch doesn't terminate Pin
Michael Walz
7:50 28 Apr '03  
GeneralRe: Batch doesn't terminate Pin
Dominik Reichl
8:49 28 Apr '03  
GeneralRe: Batch doesn't terminate Pin
Pater
23:23 28 Apr '03  


Last Updated 26 Apr 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009