how can i let my program delete itself if the user asks for it?

Started by white scorpion, January 30, 2005, 10:10:49 PM

Previous topic - Next topic

white scorpion

hi all,

i'm trying to figure out a way to write an application which can delete itself.

are there API's which i can use for it, should i use the windows installer like an uninstall program?
if so then how can i use it?

or are there other ways?

Thanks in advance for your replies :D

asmGhost


hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

petezl

The problem is that you can't delete a file that's in use. There are ways around it, have a look at this...
http://www.catch22.net/tuts/selfdel.asp
Peter.
Cats and women do as they please
Dogs and men should realise it.

Ash_

i havent checked that page link from petezl, but why not write a batch file that del's make it pause. exit your program and run the batch file?

this might work.

white scorpion

Thanks for your replies.

I know a program can not actually delete ITSELF, but what i mean /meant was that it 'triggers' something which allows the program to completely be removed from the computer.

kindof like using the RunOnce entry in the registry combining with the 'del' command.
i think this is one of my best options. or perhaps calling another process which deletes mine.


hutch--

The problem is usually associated with installations and there is no direct method to do this. A batch file will work but will display an error when you delete the batch file itself with itself. Another trick is to use a DOS 16 bit program that deletes the file you want and itself if this still works on later versions of Windows. There is supposed to be a setting that is handled by the OS at boot that will clean uip this type of junk but I forget how its done.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Nilrem

It's all explained clearly in the link that was provided earlier in this thread.

hutch--

His list misses a very simple way that works on ALL version of Windows from win 3.0 to server 2003. A 16 bit DOS exe does not show an error when it deletes itself and it can delete other files as well. here is the test written in quick basic 4.5 and compiled as a 16 bit EXE file.

kill "sdel.exe"
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

You can have a batch file that deletes everything and then deletes itself.
This will work successfully, but will return an error due to trying to read the next line of the non-existing batch file.
But as long as you don't mind a little "Batch file not found" error message, then it's okay.
No snowflake in an avalanche feels responsible.

petezl

I think Hutches solution is rather elegant as it's simple and shows no errors.
Obviously 16bit dos hasn't got the deletion restraints that windows have.
Peter.
Cats and women do as they please
Dogs and men should realise it.

pbrennick

I like Hutch's method but there must be a way of notifying Windows to delete the file after it exits.  I say this because, typically, uninstall.exe runs from the directory being removed and it is always removed along with the rest of the files.  So there must be a way and I suspect it is via a Notify message sent to the OS.

Paul

Relvinian

To All,

If you use the CreateFile() API call, you can instruct windows to delete the file upon 'close of the last handle' by passing FILE_FLAG_DELETE_ON_CLOSE.

An example CreateFile invoke would look like this:


invoke CreateFile, [FileSpec], FILE_READ or FILE_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, \
     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_DELETE_ON_CLOSE, NULL


Hope this helps.

Relvinian

white scorpion



commd       db "cmd.exe /c del ",0

processinfo PROCESS_INFORMATION <>
startup     STARTUPINFO         <>

clbuff      db 500 dup (?)

TotalCleanUp PROC

;delete registry entries
;------------------------
invoke RegOpenKeyEx,HKEY_LOCAL_MACHINE,addr regkey,0,KEY_SET_VALUE, addr hkey
.IF eax!=0
    invoke RegCloseKey,hkey
    ret
.ENDIF
invoke RegDeleteValue,hkey,addr ProcName
invoke RegCloseKey,hkey


;remove the program itself
;-------------------------

invoke GetCommandLine
mov ComLine,eax

invoke lstrcpy,addr clbuff,addr commd
invoke lstrcat,addr clbuff,ComLine
mov startup.wShowWindow,SW_HIDE
invoke CreateProcess,NULL,addr clbuff,NULL,NULL,FALSE,0,NULL,NULL,addr startup,addr processinfo

invoke ExitProcess,0

TotalCleanUp ENDP


this is what i have done, and it works like a charm :)

thanks for all your help guys, i have solved it now :D


pbrennick

Relvinian,
Thank you for that information.  I thought there must be a way, just did not know how.  It is very useful to know these things.

Cheers,
Paul