We help IT Professionals succeed at work.

Deleting an executable while it is running

chungee
chungee asked
on
Medium Priority
169 Views
Last Modified: 2010-04-06
How can I delete my executable while it is running?

Comment
Watch Question

Commented:
You can't.

Commented:
Take a lock at http://www.delphi3000.com/articles/article_514.asp

Title :
How can I remove my Exe-file while my Application is still running?

You must registered before see the answer . Is free for first 40-50 answers. After that you can unsubscribe (without pay anything) .



Commented:
Sorry for typing :P ...Take a look ...

Commented:
It is possible to delete an exe while it continues running.
You have to use low level sector access to the HD to do it and you need a LOT of knowledge about disk structure. It is a dangerous and tricky thing. I do not recommend it at all. Perhaps you would be satisfied with a method that merely appears to delete an exe while it runs... that's easier and pretty safe. You can have your exe make a copy of itself in the windows/temp dir and cause that copy to run...then shut itself down... then the copy deletes the first exe from disk... to the user this looks like the exe has deleted itself and continues to run. You might also have an exe deleted the next time windows is started up.

I have a simple delphi unit that makes it easy to do things like modify an exe while it runs... if you want to see it send me an email gacarpenter386@yahoo.com

Commented:

Hi Gwena,

  Would you send me a copy too?
new_x@yahoo.com

Regards,
Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
a simple trick to do that

procedure DeleteSelf;
var
    F: TextFile;
    batName: string;
    pi: TProcessInformation;
    si: TStartupInfo;
begin
    batName := ExtractFilePath(ParamStr(0)) + '\$$$$$$$$.bat';
    AssignFile(F,batName);
    Rewrite(F);
    Writeln(F,':try');
    Writeln(F,'del "'+ParamStr(0)+'"');
    Writeln(F,'if exist "'+ ParamStr(0)+'"'+' goto try');
    Writeln(F,'del "' + batName + '"' );
    CloseFile(F);
    FillChar(si, SizeOf(si), $00);
    si.dwFlags := STARTF_USESHOWWINDOW;
    si.wShowWindow := SW_HIDE;
    if CreateProcess( nil, PChar(batName), nil, nil, False,
IDLE_PRIORITY_CLASS,
        nil, nil, si, pi ) then
    begin
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteSelf;
end;
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- PAQ'd and points NOT refunded
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

twalgrave
Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.