No announcement yet.

run exe in memory while source on disk is deleted?

  • Filter
  • Time
  • Show
Clear All
new posts

  • run exe in memory while source on disk is deleted?

    I want to run an executable but delete it's source on disk while the exe is running. Details:

    1) I have a computer with no hard drive that runs its Windows operating system totally from CD (i.e. BartPE)
    2) I want to programmatically eject the CD and turn off the computer using eject.exe and turnoff.exe.
    3) The above programs reside on the CD, so once the CD is ejected, the second program "turnoff.exe" cannot run.

    A RAM disk is not an option for me because of the complexity and ugliness.

    I made a single exe with Powerbasic but as soon as the CD ejects, the exe cannot complete the turnoff function.

    Some have suggested UPX'ing the exe so that it will "run in memory", but my attempt to delete it from disk while running failed since a "handle" remained on it.

    Here is C language source code that supposedly works: but my limited experience prevents me from translating it to Powerbasic.

    There are many other uses for such an ability. An SFX installer could unpack itself without writing to disk, etc.

    P.S. I just purchased the latest PB Win 9.0 and CC 5.0. What wonderful tools!

  • #2
    Just a thought - create a thread in eject.exe that starts a timer and shuts down the computer when the timer elapses.


    • #3
      See remark:
      "Simple application to load an EXE file and run it from memory (only for .NET compiled files)"

      This is for .NET executables.
      Something PwrDev can do


      • #4
        You might be able to do it if you don't need any stack (that is, you handle ALL memory allocation yourself from far heap) by allocating enough memory with EXECUTE permission, POKEing or 'copymemory' the executable code for the procedure there, and doing a CALL DWORD on the address of the function.

        But I don't know how you would return to the EXE to complete the execution.

        Or maybe RUNDLL could help? I don't know if rundll does the equivalent of 'start' or if it waits for the function to return.

        But to prevent a cooperating program from running when something else is running, wouldn't creating a mutex be a lot simpler?
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]


        • #5
          2) I want to programmatically eject the CD and turn off the computer using eject.exe and turnoff.exe.
          Instead of relying on those two utilities on the CD perhaps you could add functions to your own PowerBASIC exe to do the same things?

          Using something like this to eject the CD..
             DriveLetter$ = "F:"  '<CD/DVD drive letter>
               mciSendString "open " + DriveLetter$ + ": type cdaudio alias CDName", "", 0, 0
               mciSendString "set CDName door open", "", 0, 0
               mciSendString "close CDName", "", 0, 0
          And a variation of the code provided with PBWin in the 'restart.bas' sample program to shutdown the computer.
          ExitWindowsEx(%EWX_POWEROFF OR %EWX_SHUTDOWN, 0)
          Rgds, Dave


          • #6
            You can't delete a .exe file if it's loaded as a process. An alternative is to manually load the exe, just like the operating system does, but without keeping the file open so you can delete it.

            Semen's posted some code for manually loading exes. It doesn't work with all exes but should work with most PB exes ...


            • #7
              I used Semen's code in the previous message to accomplish what I needed via his "ActivateExe" function.

              FUNCTION PBMAIN
              pid???=SHELL("cdeject.exe /x",0)
              ActivateExe "poweroff.exe"
              END FUNCTION

              poweroff.exe waits 5 seconds allowing enough time for the cdeject command (ran asynchronously) to eject the CD before power is lost.
              #COMPILE EXE
              #CONSOLE OFF
              #INCLUDE ""

              ' // Native API to Shutdown the System
              DECLARE FUNCTION RtlAdjustPrivilege& LIB "ntdll" ALIAS "RtlAdjustPrivilege" (BYVAL Privilege&, BYVAL NewValuea&, BYVAL NewThread&, OldValue&)
              DECLARE FUNCTION NtShutdownSystem& LIB "ntdll" ALIAS "NtShutdownSystem" (BYVAL ShutdownAction&)

              FUNCTION PBMAIN () AS LONG
              RtlAdjustPrivilege 19, 1, 0, 0 'Set Shutdown Privileges
              SLEEP 5000
              NtShutdownSystem 2 '0=shutdown, 1=restart, 2=poweroff
              END FUNCTION
              So far it has worked flawlessly on 10 computers: (i.e. boot computer from BartPE boot CD, then run program to eject disk and shutdown computer) This is akin to staying suspended as you sit on a limb and saw it off.