Solved

App Delete its own exe on terminate

Posted on 2004-10-26
533 Views
Last Modified: 2012-05-05
How do i make a program delete its own exe when it closes. (Without shelling another exe to delete it on Unload)
0
Question by:kurian2z5
    16 Comments
     
    LVL 5

    Expert Comment

    by:RogueSolutions
    Can you control the opening of the EXE?  If so, I'd launch a different EXE that in turn starts your actual "program" and waits for it to complete before deleting it.

    You can watch an exe via VB (don't have code to hand though) or by using Installer software such as Wise - which can launch a program with an optional "wait for exit" parameter.

    Your actual program needn't be named as an EXE if you are trying to protect it.

    There won't be a way of getting rid of your launcher app though, but without the main program it will be useless.

    Simon
    0
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You can't.  This would be like trying to pull a stool out from under your feet while you are standing on it.

    You must use some kind of external application to delete the EXE, whether it be a batch file or another application.

    ~IM
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    You could try using:

          Kill App.Path

    but I expect it will be locked, as Rogue and IM have said...

    J
    0
     
    LVL 27

    Accepted Solution

    by:
    Hi

    Private Sub DeleteMe()
       Open App.Path + "\Delself.bat" For Append As #1
       Print #1, "@echo off"
       Print #1, ":try"
       Print #1, "del " + App.EXEName + ".exe"
       Print #1, "if exist " + App.EXEName + ".exe goto try"
       Print #1, "del " + App.Path + "\Delself.bat"
       Close
       Shell App.Path + "\Delself.bat", vbHide
    End Sub

    Private Sub Form_Unload()
       DeleteMe
    End Sub
    0
     
    LVL 8

    Assisted Solution

    by:List244
    Ark has the right idea to answer your question,
    however there are a few mistakes, let me
    correct them for you:
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private Sub DeleteMe()
    Open App.Path & "\Delself.bat" For Output As #1
       Print #1, "@echo off"
       Print #1, ":try"
       Print #1, "del " & App.EXEName & ".exe"
       Print #1, "if exist " & App.EXEName & ".exe goto try"
       Print #1, "del """ & App.Path & "\Delself.bat" & """;"
    Close #1
       Shell App.Path + "\Delself.bat", vbHide
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
       DeleteMe
    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    First error fixed:
    Unload must have the (Cancel As Integer) ' To prevent errors

    Second error fixed:
    He had a slight error in his batch coding, which made it so the
    batch never deleted. Which, kind of ruins the point of self-deletion.
    In order for it to delete itself correctly the filename must be in quotes.

    Simply add the above code to your form, and it will do the rest for
    you, then your EXE will self-destruct.
    0
     
    LVL 4

    Author Comment

    by:kurian2z5
    Are you sure there is no way to make an app delete itself without using another external program ot bat ? BTW how do you shell an app not named .exe like Rouge mentioned, like the .icd encrypted file Safedisc 1 used ?
    0
     
    LVL 5

    Expert Comment

    by:RogueSolutions
    You can't have an EXE delete itself as others have indicated.  To execute the deletion the EXE must be running therefore the deletion will fail because the file is in use.

    ---

    The Visual Basic command Shell assumes that the parameter given is an EXE and runs it (have just tested this - on Win2K at least)

    So ...

         Shell "C:\myfile.ext", vbNormalFocus

    will run myfile.ext (or whatever you want to call your file).  Doesn't matter if the ext is an existing one, e.g. txt

    The name Shell in VB is a bit misleading.

    ---

    To achieve your goal you'll need two apps, one to run/watch and/or delete and your actual app.

    Personally I think the self-creating BAT file proposed by Ark / List244 is a very tidy solution.  



    0
     
    LVL 8

    Expert Comment

    by:List244
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    ShellExecute Me.hwnd, "OPEN", "FILENAME", "", "", 1

    That will launch another file type.
    0
     
    LVL 4

    Author Comment

    by:kurian2z5
    Btw List244, i wanted to know how to execute an .exe file that has been renamed to some other extension (execute it like a program).

    Like in SafeDisc 1, the exe is encrypted and stored as a .icd file. When the game is run using the launcher exe, the icd file is decrypted and executed from memory without being actually written to the harddisk as a new file. When i view running processes it shows the game.icd is running.

    How do i do this ?
    0
     
    LVL 8

    Expert Comment

    by:List244
    The VB shell function will launch an exe that is not named
    .exe.  As far as unencrypting the exe, can't help you there.
    0
     
    LVL 5

    Expert Comment

    by:RogueSolutions
    Kurian - How do you know the icd file is encrypted?  

    It could well be an normal exe that expects a particular command-line parameter to make it work (so if you just renamed it to .exe and ran it manually that would fail).

    The launcher app you mention does the clever 'protection' stuff.  You can mimic this sort of thing with two VB apps - one calls the other with a 'key' of some sort.  Not sure why you'd want to of course.

    0
     
    LVL 4

    Author Comment

    by:kurian2z5
    I didnt know you could shell an executable that is not named .exe, i tried it and it works.
    But how do decrypt an encrypted file and execute it from memory without having to save it to a file ?
    0
     
    LVL 5

    Expert Comment

    by:RogueSolutions
    Kurian

    There are plenty of tricks people can pull that might look like encryption that aren't.

    1) Say I give you a file called MyTrick.XXX

    2) Now really it is an EXE that will only run if passed a command-line parameter of "PleaseRunMeNow".  if it doesn't get that command line it doesn't run.

    Unless I tell you part (2) you'll never know how to get it going so it 'encrypted' as far as you are concerned

    ---

    If their file really is and encrypted file that is run from within memory then that's very clever but why bother?


    0
     
    LVL 4

    Author Comment

    by:kurian2z5
    SafeDisc is a CD copyprotection method for games. There is a dummy exe and a .icd file. You have to run the dummy exe, it checks for the original Disc and then the .icd file is decrypted and run.

    There are tools like UnSafeDisc that decrypt the .icd file and give you the original exe which you can run.

    I just used this as an example. I want to encrypt a third party exe so that my launcher program will decrypt it and run it only if you provide a correct password to the launcher or something. So It should be independent of the exe Im encrypting.

    One more thing. I can shell an exe renamed to another extension with VB's shell function, but how do i run it from windows ?
    0
     
    LVL 8

    Expert Comment

    by:List244
    If you encrypt it yourself its easy.
    Go to google, and search an encryption method that you like.
    Encrypt the exe then have another program that decrypts
    it if the password is correct. If you need more help than this
    it is time to open a new question. Rules of EE do not allow
    changing your question to get more answers. The point system
    has a reason.

    0
     
    LVL 4

    Author Comment

    by:kurian2z5
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now