Uninstall program that can remove itself

How can I make it so that my uninstall program can remove itself after it's done with all the other files?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BTW, another method would be to


This would schedule your file to be deleted when the machine reboots the next time.
CornDog932Author Commented:
Thank you for the great solutions
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.