Menu

Delete an .exe file on termination

CLMan
2007-10-20
2012-09-26
  • CLMan

    CLMan - 2007-10-20

    In a Dev-C++ project, I want to run an .exe file (simple.exe in TEMP folder) and delete the file (after the project is terminated) when simple.exe is closed.

    Is it possible managing this while the project is not running.

    char TempFileName[MAX_PATH + 1];
    TempFileName[MAX_PATH] = '\0';

    GetTempPath(MAX_PATH, TempFileName);
    strcat (TempFileName, "simple.exe");

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    CreateProcess(NULL, TempFileName, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

    // Here the main project .exe is terminated

     
    • Dave Turner

      Dave Turner - 2007-10-29

      doh, sorry - I meant "simply have your executable create and run that batch file and the .bat file will continue running"
      (didnt have my Red Bull this morning)

       
    • Anonymous

      Anonymous - 2007-10-20

      A process cannot delete itself after it has terminated (because it is not running!). However a process can delete a file of course, so you can have one process run another and then delete it when it terminates - i.e. you need two executables.

      However, why would you want to do such a thing? It is trivial so you should not need any help, but you won't get much support for writing your suspiciously behaving code here!

       
    • CLMan

      CLMan - 2007-10-20

      I am afraid I couldn't express myself truly.

      I have an password protected .exe file which has two parts. First part of it is an extractor .exe stub file and the second part is an encrypted .exe file.

      If the user enter correct password, stub file decrypts the second part of the file, save it to a temporary folder and run it.

      Since the stub file is terminated while decrypted .exe is running, I can not delete it. I am looking for an alternative way of managing this.

       
    • Anonymous

      Anonymous - 2007-10-20

      Why terminate the stub file? Get it to wait until the created process terminates then delete the file.

      If you launch the decrypted .exe using one of the spawn functions with mode = _P_WAIT ( http://msdn2.microsoft.com/en-us/library/20y988d2(VS.71).aspx ), the spawn function does not return until the spawned process terminates.

      A slightly more convoluted method if you don't want the starter process hanging around is to create a third executable that repeatedly attempts to delete the temporary executable until it succeeds, and have this launched by the temporary itself just before it terminates.

      Clifford

       
    • CLMan

      CLMan - 2007-10-21

      I am going to try to decrypt the file to memory (hard work). On failure, the most preferable method is not to terminate the stub file and using a spawn function even if it is not a practical way for the project.

      Thank you very much for your kind help!

       
      • Anonymous

        Anonymous - 2007-10-21

        If you are going to decrypt directly to memory, you will have to replace the functionality of the OS loader, which is no doubt not as simple as reading the file to memory and jumping to the start location. It may also look like at attempt to execute a data segment - an action that indicates either buggy or malicious code. All recent Intel processors and Microsoft OSs (Win2k or later) have have hardware/software protection mechanisms to prevent a process from executing code in a data segment.

        There are ways of making the starter stub hide itself so that there is no visible window while it waits for the decrypted process to run. If this is a GUI app. it is especially simple.

        Clifford

         
        • Anonymous

          Anonymous - 2007-10-22

          You may need this is you are going to attempt to build your own program loader: http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx

           
    • CLMan

      CLMan - 2007-10-23

      It is beyond doubt that I need this for the project. Thanks again!

       
    • Dave Turner

      Dave Turner - 2007-10-29

      .exe's can't delete themselves while they're running .... but .bat's can.

      It's not the most elegant solution but it works ... example -

      :DeleteFile
      del c:\myexe.exe
      if exist c:\myexe.exe goto DeleteFile
      del c:\delmyexe.bat
      exit
      --
      Simply have your .bat file create and run that executable and the .bat will continue running in a loop until c:\myexe.exe has been deleted, at which stage it will delete the .bat file also.

       
      • Anonymous

        Anonymous - 2007-10-29

        ... or have your executable create and run the batch file.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.