• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1006
  • Last Modified:

Delete a file upon exit, the own executable file.

Hi,

Let said i have run.exe, is it possible to delete run.exe upon exit of run.exe? meaning deleting it self after exit from the app. I know it is quite impossible but is there any way around that possible made up for this?

Regards
0
iphone
Asked:
iphone
4 Solutions
 
dbkrugerCommented:
You can have a batch script that deletes the file after running it.

foo.bat
==========
run.exe
delete run.exe
0
 
ozoCommented:
It depends on the operating system.
0
 
WoodsterCommented:
You can use the spawn function to spawn a secondary application that will delete the first application.

You can then set the secondary application to take 2 parameters, being the file to be deleted and a wait period allowing the first file to terminate successfully.

Something like:

app1

void main (void)
{
  /* Stuff here */
  spawnl(P_NOWAITO, "app2.exe", 2);
}

app2

void main (int argc, char *argv[])
{
  /* Wait required number of seconds */
  _sleep(atoi[argv[1]];

  /* Delete the file */
  _unlink(argv[0]);
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jkrCommented:
And how do you get rid of the batch file ? ;-)

/********************************************************************
Module Name:    DeleteMe.cpp Author:        Jeffrey Richter
#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 ther Clone?
   // The original EXE receives one argument,
   // the clone more.
   if (__argc == 1) {

      // Original EXE: Start the Clone.

      // Copy EXE to the temporary 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 using FILE_FLAG_DELETE_ON_CLOSE
      HANDLE hfile = CreateFile(szPathClone, 0, FILE_SHARE_READ, NULL,
                           OPEN_EXISTING,FILE_FLAG_DELETE_ON_CLOSE);

      // Start the Clone. Pass the PID and the fully qualified path
      // of the original 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);

      // now the original process may terminate
      } else {

      // Clone-EXE: Delete the  original EXE after termination
      HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]);
      WaitForSingleObject(hProcessOrig, INFINITE);
      CloseHandle(hProcessOrig);
      DeleteFile(__targv[2]);
   }
   return(0);
}

Code taken from http://www.microsoft.com/msj/0198/win320198.aspx - http://www.microsoft.com/msj/0198/win32textfigs.htm#fig1 to be precise, the scoop is how to how to write an unsetup program that was able to delete itself.
0
 
Infinity08Commented:
A few thoughts :

1) Use a loader exe to execute the program. Once the program finishes, the loader can remove it. But there is still the loader that remains in that case

2) place the application in a temporary directory (that gets cleaned out regularly) ... the EXE won't be deleted immediately after execution, but it will be at some point in the future (depending on your goal, this might be ok)

3) Under Windows, you can mark a file for deletion when the system boots the next time :

      MoveFileEx(your_exe, NULL, MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/movefileex.asp

An article that is related :

      http://support.microsoft.com/?id=140570
0
 
rstaveleyCommented:
Need to figure out the time in a minute's time (let's say it is 10:00) and run the following in an at exit function:

   system("at 10:00 erase c:\\bin\\run.exe");
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now