Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A program which can delete itself

A program which can delete itself

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zengkun100
    wrote on last edited by
    #1

    Many years ago, Jeffrey Richter wrote a program to show how to delete a program itself when it was closed. It sounds very funny, and I tried this program yesterday. I saidly found that the program does deleted but the temp file still exist. It seems that FILE_FLAG_DELETE_ON_CLOSE flag doesn't work on a exe file. Here is the source code.Any one knowns why the temp file doesn't get deleted? :sigh: int WINAPI _tWinMain(HINSTANCE h, HINSTANCE b, LPTSTR 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, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 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, GetCurrentProcessId()); wsprintf(szCmdLine, __TEXT("%s %d \"%s\""), szPathClone, hProcessOrig, szPathOrig); STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); CloseHandle(hProcessOrig); CloseHandle(hfile); // This original process can now terminate. } else { // Clone EXE: When original EXE terminates, delete it HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]); WaitForSingleObject(hProcessOrig, INFINITE); CloseHandle(hProcessOrig); DeleteFile(__targv[2]); // 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 } return(0); }

    A Chinese VC++ programmer

    H 1 Reply Last reply
    0
    • Z zengkun100

      Many years ago, Jeffrey Richter wrote a program to show how to delete a program itself when it was closed. It sounds very funny, and I tried this program yesterday. I saidly found that the program does deleted but the temp file still exist. It seems that FILE_FLAG_DELETE_ON_CLOSE flag doesn't work on a exe file. Here is the source code.Any one knowns why the temp file doesn't get deleted? :sigh: int WINAPI _tWinMain(HINSTANCE h, HINSTANCE b, LPTSTR 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, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 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, GetCurrentProcessId()); wsprintf(szCmdLine, __TEXT("%s %d \"%s\""), szPathClone, hProcessOrig, szPathOrig); STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); CloseHandle(hProcessOrig); CloseHandle(hfile); // This original process can now terminate. } else { // Clone EXE: When original EXE terminates, delete it HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]); WaitForSingleObject(hProcessOrig, INFINITE); CloseHandle(hProcessOrig); DeleteFile(__targv[2]); // 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 } return(0); }

      A Chinese VC++ programmer

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      I didnt try to it but maybe you can run a batch file and on that file write for delete your file and itself when you want to close your program.

      Z 1 Reply Last reply
      0
      • H Hamid Taebi

        I didnt try to it but maybe you can run a batch file and on that file write for delete your file and itself when you want to close your program.

        Z Offline
        Z Offline
        zengkun100
        wrote on last edited by
        #3

        Thank you! :) What you said is one of the many ways to delete a program itself.

        A Chinese VC++ programmer

        H 1 Reply Last reply
        0
        • Z zengkun100

          Thank you! :) What you said is one of the many ways to delete a program itself.

          A Chinese VC++ programmer

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          You're welcome. :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups