Uninstall program that can remove itself

Posted on 2004-11-16
Medium Priority
Last Modified: 2013-12-03
How can I make it so that my uninstall program can remove itself after it's done with all the other files?
Question by:CornDog932
  • 2
LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 12594726
Jeffrey Richter demonstrated that in http://www.microsoft.com/msj/0198/win320198.aspx ("Win32 Q & A -- MSJ, January 1998"). The trick is to spawn a copy of your own process that is created with 'FILE_FLAG_DELTE_ON_CLOSE' to wipe out the original executable:

 Module name: DeleteMe.cpp
 Written by: Jeffrey Richter
 Description: Allows an EXEcutable file to delete itself
 #include <Windows.h>
 #include <stdlib.h>
 #include <tchar.h>
 int WINAPI WinMain(HINSTANCE h, HINSTANCE h2, LPSTR psz, int n) {
    // Is this the original EXE or the clone EXE?
    // If the command-line 1  argument, this is the original EXE
    // If the command-line >1 argument, this is the clone EXE
    if (__argc == 1) {
       // Original EXE: Spawn clone EXE to delete this EXE
       // Copy this EXEcutable image into the user's temp directory
       TCHAR szPathOrig[_MAX_PATH], szPathClone[_MAX_PATH];
       GetModuleFileName(NULL, szPathOrig, _MAX_PATH);
       GetTempPath(_MAX_PATH, szPathClone);
       GetTempFileName(szPathClone, __TEXT("Del"), 0, szPathClone);
       CopyFile(szPathOrig, szPathClone, FALSE);
       // Open the clone EXE using FILE_FLAG_DELETE_ON_CLOSE
       HANDLE hfile = CreateFile(szPathClone, 0, FILE_SHARE_READ, NULL,                               
       // Spawn the clone EXE passing it our EXE's process handle
       // and the full path name to the original EXE file.
       TCHAR szCmdLine[512];
       HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE,                                               
       wsprintf(szCmdLine, __TEXT("%s %d \"%s\""), szPathClone, hProcessOrig,                     
       STARTUPINFO si;
       ZeroMemory(&si, sizeof(si));
       si.cb = sizeof(si);
       CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
       // This original process can now terminate.
       } else {
       // Clone EXE: When original EXE terminates, delete it
       HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]);
       WaitForSingleObject(hProcessOrig, INFINITE);
       // Insert code here to remove the subdirectory too (if desired).
       // The system will delete the clone EXE automatically
       // because it was opened with FILE_FLAG_DELETE_ON_CLOSE
LVL 86

Expert Comment

ID: 12595115
BTW, another method would be to


This would schedule your file to be deleted when the machine reboots the next time.

Author Comment

ID: 12595641
Thank you for the great solutions

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question