App Delete its own exe on terminate

How do i make a program delete its own exe when it closes. (Without shelling another exe to delete it on Unload)
LVL 4
kurian2z5Asked:
Who is Participating?
 
ArkCommented:
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
 
RogueSolutionsCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jimbobmcgeeCommented:
You could try using:

      Kill App.Path

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

J
0
 
List244Commented:
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
 
kurian2z5Author Commented:
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
 
RogueSolutionsCommented:
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
 
List244Commented:
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
 
kurian2z5Author Commented:
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
 
List244Commented:
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
 
RogueSolutionsCommented:
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
 
kurian2z5Author Commented:
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
 
RogueSolutionsCommented:
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
 
kurian2z5Author Commented:
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
 
List244Commented:
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
 
kurian2z5Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.