Uninstall program that can remove itself

Posted on 2004-11-16
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
    LVL 86

    Accepted Solution

    Jeffrey Richter demonstrated that in ("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

    BTW, another method would be to

    MoveFileEx("myapp.exe", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);

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

    Author Comment

    Thank you for the great solutions

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
    What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
    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.…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now