Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I am EXE file. How do i delete myself while i am running.

73 views
Skip to first unread message

Stanimir

unread,
Nov 23, 2001, 4:10:02 AM11/23/01
to

I want to delete exe file while is opened. My friend did that in Visual Basic with KILL function and it worked. I tried with DeleteFile function but Access denied error occured.

Gwen Carpenter

unread,
Nov 23, 2001, 6:36:33 AM11/23/01
to
In article <3bfe126a$1_2@dnews>, sa_ml...@dir.bg says...

>
> I want to delete exe file while is opened. My friend did that in Visual Basic with KILL function and it worked. I tried with DeleteFile function but Access denied error occured.
>
This is very nearly impossible in delphi
You can write to disk sectors directly using delphi so it is possible
to overwrite the exe file's data on disk as it runs... and then to clean
up the fat and make everything as it would be if you just deleted the exe
while it was not running... but you would really need a lot of intimate
knowledge of the layout of the HD. I have deleted/modified an exe's file
as it ran using direct writes... but I found it to be more trouble than
it's worth.. I suggest spawning a second exe (an image of the first) into
windows/temp dir ... then use it to shut down and delete the
original...it leaves a file in the temp dir .. but it's a lot easier to
do than direct writes. of course you will need to think about sending
the spawned copy of the exe all necessary data about the current state of
the original... like screen pos,variable values...etc ..so that when it
takes over for the original it does so seamlessly :-)

Chirag Dalal

unread,
Nov 23, 2001, 7:10:38 AM11/23/01
to
Is that true!? I tried it in VB6 and it failed with

---
Run-time error '75':

Path/File access error
---

Which file did your friend delete?

There are methods using which an EXE can delete itself but its not as
straight-forward as using DeleteFile(). One of the methods involves using a
batch file that deletes the running exe file. You may want to generate this
batch file in code:

deleteme.bat
---
:deleteit
del "<your exe file name with full path here>"
if exist "<your exe file name with full path here>" goto deleteit
del deleteme.bat
---

Some points to note here:
1. This batch file deletes itself after deleting the exe.
2. Make sure not to leave any line after the "del deleteme.bat" line.

You may want to create this batch file in some temporary folder.
ShellExecute() this batch file with SW_HIDE as the show parameter.

I hope this helps.

- Chirag
http://chiragdalal.tripod.com/

TAnimationFX - Perform animation effects on images
http://chiragdalal.tripod.com/animfx.html

Stanimir <sa_ml...@dir.bg> wrote in message news:3bfe126a$1_2@dnews...

Johnnie

unread,
Nov 23, 2001, 7:19:33 AM11/23/01
to
>I want to delete exe file while is opened. My friend did that in Visual
>Basic with KILL function and it worked. I tried with DeleteFile function
>but Access denied error occured.

from Krasimir Stoyanov found in this groups.

[\Quote]
I converted this to Delphi and can confirm it works on Win95, Win NT 4.0 and Windows
2000: The poster tested it on Windows 98, so remainsWindows XP, anyone?

procedure DeleteSelf;
var hModule:THandle;
szModuleName:array[0..MAX_PATH] of char;
hKrnl32 : THandle;
pExitProcess, pDeleteFile, pFreeLibrary,pUnmapViewOfFile : pointer;
ExitCode:UINT;
begin
hModule:= GetModuleHandle(nil);

GetModuleFileName(hModule, szModuleName, sizeof(szModuleName));


hKrnl32 := GetModuleHandle ( 'kernel32' );
pExitProcess := GetProcAddress ( hKrnl32, 'ExitProcess' );
pDeleteFile := GetProcAddress ( hKrnl32, 'DeleteFileA' );
pFreeLibrary := GetProcAddress ( hKrnl32, 'FreeLibrary' );
pUnmapViewOfFile := GetProcAddress ( hKrnl32, 'UnmapViewOfFile' );

ExitCode := system.ExitCode;

if($80000000 and GetVersion())<>0 then
// Win95, 98, Me
asm

lea eax, szModuleName
push ExitCode
push 0
push eax
push pExitProcess
push hModule
push pDeleteFile
push pFreeLibrary
ret
end
else
begin
CloseHandle(THANDLE(4));

asm
lea eax, szModuleName
push ExitCode
push 0
push eax
push pExitProcess
push hModule
push pDeleteFile
push pUnmapViewOfFile
ret
end
end
end;

[Quote/]


As far as I know it doesn't work on XP.

Regards
Johnnie.

Johnnie

unread,
Nov 23, 2001, 7:33:49 AM11/23/01
to
By the way make sure that this is the last call on your project
or you might have problems. The best place to put is in the
project source code just after the Application.Run statement.

regards
Johnnie.

Stanimir

unread,
Nov 23, 2001, 8:45:02 AM11/23/01
to

"Chirag Dalal" <dalal....@iname.com> wrote:
>Is that true!? I tried it in VB6 and it failed with
>
>---
>Run-time error '75':
>
>Path/File access error
>---
>
>Which file did your friend delete?
>

Yes, it is true.
Here is his code on VB3.

In Main form just run this code:

Unload Me
Kill app.Path + "\" + app.EXEName + ".EXE"

Tested on Windows 98!!!

Thanks about the idia for Batch file.


Stanimir

unread,
Nov 23, 2001, 8:43:33 AM11/23/01
to

Stanimir

unread,
Nov 23, 2001, 8:56:58 AM11/23/01
to

Thank you very much Johnnie.
It works perfect. You helped me a lot.

Johnnie

unread,
Nov 23, 2001, 10:40:28 AM11/23/01
to
>
>Thank you very much Johnnie.
>It works perfect. You helped me a lot.
>

Thank you for your kind words but I'm not to be thanked here.

You must thank Krasimir Stoyanov and a number of other
developers (that I do not recall at the moment ) that have
spend a significant amount of time to produce this procedure.

Regards
Johnnie.

ganesh

unread,
Nov 27, 2001, 7:51:30 AM11/27/01
to

Could u pls tell me where should i put this code, i don't know how to insert in the project

ganesh

Ömür Ölmez

unread,
Nov 26, 2001, 5:41:42 PM11/26/01
to
There is a Standart Windows Installation Support for the dll's etc. is run
[in memory, couldn't owerrite] when installing process.
A wininit.ini file in windows\ or windows\system. Installation software adds
some records to this file like this :
del=old.dll
ren=new.bak new.dll

A console program runs before Windows loads any lib, check if it is a
wininit.ini and process the script in it. Then, deletes the old.dll in the
example and renames new.bak to new.dll. At the and, renames the wininit.ini
to wininit.bak.
When this happens, windows shows a text message like this :
Updating System Config. This take a few time . . .

Refer to Installation Section of SDK and look at that files in your win
directories.
Ömür Ölmez


Loren Pechtel

unread,
Nov 29, 2001, 4:51:55 PM11/29/01
to
On 23 Nov 2001 01:10:02 -0800, "Stanimir" <sa_ml...@dir.bg> wrote:

It's amazing the recent advances in AI! Here we have an AI good
enough to ask a reasonable question and what does it want to do but
commit suicide?!

Paolo Gelato

unread,
Nov 30, 2001, 3:44:08 AM11/30/01
to
Maybe it just realized it was doomed to be bugged, as any piece of software
(including human mind...)


Dan Donoghue

unread,
Nov 30, 2001, 3:32:12 PM11/30/01
to
You cant really delete it whilst its running, unless u can play around with
the memory and keep everything cached somehow.

What you can do is add an entry to
HkLM\Software\Microsoft\Windows\CurrentVersion\RunOnce (in the registry) to
run a delete command once window stars up.

Other than that you could have a monitor process checking to see if your app
is running and as soon as it stops, delete it.

--
Dan


Paolo Gelato wrote in message <3c07472a$2_2@dnews>...

0 new messages