Deletefile can't delete this exe?

Blitz3D Forums/Blitz3D Programming/Deletefile can't delete this exe?

jfk EO-11110(Posted 2006) [#1]
Is that true, a running exe cannot delete itself?

How would you solve this?

EG. my app is named test.exe and it sould contain this:
DeleteFile "test.exe".

The code is executed, but the file isn't really deleted as it should. Maybe the file is locked during execution? Can i unlock it?


H&K(Posted 2006) [#2]
Is that true, a running exe cannot delete itself?
I would imaging that windows will deny access to it.
How would you solve this?
Get it to run an other EXE then close itself down, then the other exe waits a bit then deletes the first exe. {That isnt to say this would work, its just how I would go about solving it}


b32(Posted 2006) [#3]
I think you need the MoveFileEx api: http://support.microsoft.com/kb/140570


Fuller(Posted 2006) [#4]
Why would you want to delete the .exe?


jfk EO-11110(Posted 2006) [#5]
It's the uninstaller exe. Finally the whole diskspace should be released. So a second exe would still be left back.
B32 - thanks, will read now.


jfk EO-11110(Posted 2006) [#6]
Ok it seems MoveFileEx is used to remove files that are locked by the system because they are system drivers etc. This will be executed when the machine boots the next time.

My exe is loaded, then executed. The file is not touched anymore. So in theory it should be no problem to delete it, unless an exe works as some kind of streaming program code that is executed before it's fully loaded.

The deletion of files in the boot sequence is a bit of an overkill for a simple screensaver imho.

Maybe I can use a smaller uninstaller exe, not a blitz exe. So when there are a few kb wasted on the users disk, who cares. Maybe a batch file.


jfk EO-11110(Posted 2006) [#7]
THis is really getting funny. Now I wa able to use execfile a bachfile that would delay 2 seconds, then delete the calling exe. Problem: there seems to be no good command in msdos to wait for a certain time. "Choice" can be used, but AFAIK it' snot suported by NT, 2K, XP. A very good tutorial then suggests to use the PING command (ping -n 2 127.0.0.1>NUL). THis really works, but a firewall alert will pop up - no way.

I think I really got to use an other win32 compiler. BlitzMAX would be perfect, but I didn't buy it yet but decided to wait for the Max3D modules.

Ah, the sooner or later I will nail this. :/


Rroff(Posted 2006) [#8]
I've never seen a windows machine including xp and 2k that choice wasn't present on...


OJay(Posted 2006) [#9]
why not just use a thirdparty installer like innosetup or something? they care about all installation/remove processes so you dont have to reinvent the wheel...


jfk EO-11110(Posted 2006) [#10]
Yes, maybe I should use innosetup. Neverless, reinventing the wheel may be useful sometimes. I also thought a fullfledged installer isn't neccesary for a simple SCR file.


bregors(Posted 2006) [#11]
.


H&K(Posted 2006) [#12]
It's the uninstaller exe. Finally the whole diskspace should be released. So a second exe would still be left back
Not if the second exe is the installed application. You start the application from the installer with a command line dir to the path it was installed from, then it deletes the installer and closes. When you run the application for real, there is no command path, and so it doesnt delete anything or exit, but runs the program.

Also, as your application is going to have an uninstaler you could just use that as the second exe and not the main exe. That is, if the uninsatler is run and passed the dir of the installer it deletes the instaler and closes down.


jfk EO-11110(Posted 2006) [#13]
H&K - We're talking about a SCR, not EXE. I cannot run the SCR file with a commandline argument, windows will remove it and use "/S" instead.

Deleting the installer is not an issue. It's the uninstaller that needs to be removed. So no matter how you turn it around, one exe will always be left back. The proper solution would be to use MoveFileEx and make windows delete the file during the next OS loading sequence.

I'll probably add this later, right now I'm tired of the bad puter karma I got these days. It's like I'm doomed. Sometimes everything goes quick, but when you have a bad karma phase then every little stupid bug takes days to be fixed.

BTW yes, Innosetup is powerful. Now I just spent an hour or so to make it install my screensaver, including the call to rundll32.exe to open the desk.cpl with screensaver settings etc. It compiled without errors, but the setup file didn't work. I just ain't got the patience to read trough tons of gui elements with more or less cryptic labels. Maybe I'll look into that one an other day.

Thanks for your help everyone.


H&K(Posted 2006) [#14]
Ahhhh, yep. Shutting up.

Edit: ohohohoho. How about.... the program copies itself to the windows temp dir, then runs the copy and closes. Then the copy, which knows its the copy, cos it in the temp dir, runs and it can delete the original.

OK so a copy now exists in temp, but What the hell, loads of things exist in temp. And the next time you do a system cleanup, it just get deleted.


jfk EO-11110(Posted 2006) [#15]
good point!


Kev(Posted 2006) [#16]
this might help

http://www.codeproject.com/file/cpselfdestruct2.asp


b32(Posted 2006) [#17]
The article says that .bat files can remove themself. I tried running the .scr file from CMD, and the commandline isn't changed into "/S". I have WinXPHE and I don't know if other versions of Windows do the same, but maybe you could use a .bat file for the uninstall.


Subirenihil(Posted 2006) [#18]
Self-Destructing .exe