Warren
Warren

Reputation: 803

How to make the program kill itself in delphi?

I found a post about how to kill the program itself one year ago. It suggested writing some values in registry or windows directory or a location in disk when it runs first time. When it tries to run for the second time, the program just check the value in that location, if not match, it terminates itself.

This is simple and a little naive as any realtime anti-virus application would easily watch what value and where your program wrote in a disk. And in a true sense, that method did not 'kill' itself, the program just lies thare and sleeps intact and complete, only because of lack of trigger.

Is there a method that, in true meaning, kills itself such as deleting itself permanently, disemboweling itself, disrupting classes or functions or fragmenting itself?

Thank you.

Upvotes: 1

Views: 1977

Answers (2)

jimsweb
jimsweb

Reputation: 1082

+1 to this question. It is so unfortunate that people often tend to vote down, if somebody asks questions that are related to tricky ways of doing things! Nothing illegal but at times this qustion may sound to other people that this method is unnecessary. But there are situations where one wants to delete itself (self) once it is executed.

To be clear - it is possible to delete the same exe once it is executed.

(1) As indicated in the earlier answer, it is not possible for an exe to get deleted once it is executed from disk. Because OS simply doesn't allow that.

(2) However, at this point, to achieve this, what we need to do is, just execute the EXE in momory! It is pretty easy and the same EXE could be easily deleted from disk once it is executed in memory.

read more on this unconventional technique here:

execute exe in memory

Please follow above post and see how you can execute an exe in momory stream; or you can even google it and find out yet another way. There are numerous examples that shows how to execute an exe in memory. Once it is executed, you can safely delete it from disk.

Hope this throws some light into your question.

Upvotes: 1

Ian Goldby
Ian Goldby

Reputation: 6174

An application cannot delete itself off the disk directly, because while the application is running the disk file is 'open' - hence it cannot be deleted.

See if MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT fits your requirement.

If you can't wait for a reboot, you'll have to write a second application (or batch file) that runs when the first application closes to wait for the first application to complete closing and then delete it.

It's chicken and egg though - how do you delete the second application/batch file? It can't delete itself. But you could put it in the %temp% directory and then use MoveFileEx() to delete it next time the machine is rebooted.

Upvotes: 1

Related Questions