[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

App Delete its own exe on terminate

Posted on 2004-10-26
16
Medium Priority
?
568 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
Comment
Question by:kurian2z5
  • 5
  • 4
  • 4
  • +3
16 Comments
 
LVL 5

Expert Comment

by:RogueSolutions
ID: 12411863
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 86

Expert Comment

by:Mike Tomlinson
ID: 12411869
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
ID: 12412049
You could try using:

      Kill App.Path

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

J
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 28

Accepted Solution

by:
Ark earned 200 total points
ID: 12412292
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
List244 earned 80 total points
ID: 12413700
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
ID: 12420080
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
ID: 12420718
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
ID: 12423082
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
ID: 12433489
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
ID: 12434758
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
ID: 12434898
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
ID: 12445788
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
ID: 12446171
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
ID: 12450895
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
ID: 12454945
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
ID: 12456052
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

590 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