Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

how to delete itself

hi!

i think there is an api-command or so, that let's me delete the application, which is already running and sents this deletion command, how to do this ?
like this:
starting project1.exe
project1.exe
  DeleteFile('project1.exe')
end;

but that doesn't work, because windows uses the exe-file, i think it's opened.
 
0
geoka
Asked:
geoka
  • 5
  • 3
  • 3
  • +6
1 Solution
 
lgmmCommented:
you cant do that!!!!!

the file is open if a excel file o word file is open you cant delete it

in a network you cant delete the aplications in use

in the same machine it is impossible

but you can create an extra file that allows your aplication to run and you can delete this file and your aplication cant run

if fileexist(example.dat) then
  your code();
 deletefile(example.dat)
else
  halt;




ok?
0
 
ckanetaCommented:
yeah as long as the file is open you can't delete it

that command does work however if you were to have a second project
project2.exe which deleted project1
as long as project1 isn't running.

so just make another program
project2 which runs project1 and waits for it to end, then deletes it
0
 
shenqwCommented:
From http://www.gislab.ecnu.edu.cn/delphibbs/
Author: cAkk(My Friend)

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  BatFile:TextFile;
begin
  AssignFile(BatFile,ExtractFilePath(Application.ExeName)+'DelMe.Bat');
  ReWrite(BatFile);
  WriteLn(BatFile,'@echo off');
  WriteLn(BatFile,':Loop');
  WriteLn(BatFile,'Del '+Application.ExeName);
  WriteLn(BatFile,'IF EXIST '+Application.ExeName+' goto Loop');
  Writeln(BatFile,'Del DelMe.bat');
  WriteLn(BatFile,'@echo on');
  CloseFile(BatFile);
  WinExec(PChar(ExtractFilePath(Application.ExeName)+'DelMe.Bat'),SW_HIDE);
end;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
shenqwCommented:
From http://www.gislab.ecnu.edu.cn/delphibbs/
Author: cAkk(My Friend)

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  BatFile:TextFile;
begin
  AssignFile(BatFile,ExtractFilePath(Application.ExeName)+'DelMe.Bat');
  ReWrite(BatFile);
  WriteLn(BatFile,'@echo off');
  WriteLn(BatFile,':Loop');
  WriteLn(BatFile,'Del '+Application.ExeName);
  WriteLn(BatFile,'IF EXIST '+Application.ExeName+' goto Loop');
  Writeln(BatFile,'Del DelMe.bat');
  WriteLn(BatFile,'@echo on');
  CloseFile(BatFile);
  WinExec(PChar(ExtractFilePath(Application.ExeName)+'DelMe.Bat'),SW_HIDE);
end;
0
 
veeramaniCommented:
Hi,
   press ctrl+alt+del
 then end task
then press end task
finish
regards,
veeramani
0
 
geokaAuthor Commented:
yours answer is not suitable, in the end, no file should be left.
the comment of schengw seams pretty good, but there's one trap, i think. you can't delete en application while it's running, correct ???
but that is what the patch trys to do. or is the application already closed, when the patch has launched ??
would be great...

to all, especially ti igmm: please post in comments first, there's no need to give an answer, if you don't really understand/know what i really want, just i mean, it doesn't helps very much if the exec. is deleted, but a file remains still on disk, that couldn't be the best way.
0
 
shenqwCommented:
Yeah.If the application is running ,it can't work,but the application close ,the file will be deleted.So i place the code in the close event of mainform  (Form1).
0
 
ptmcompCommented:
You can insert a regsitry key in the \Software\Microsoft\Windows\CurrenVersion\RunOnce that deletes your app.
0
 
RadlerCommented:
Hi geoka,

I've a code in C to do this
See some hints at

http://www.microsoft.com/msj/0198/win320198.htm

http://support.microsoft.com/support/kb/articles/q140/5/70.asp



/********************************************************************
        Module Name:    DeleteMe.cpp Author:        Jeffrey Richter
        #include <Windows.h>
        #include <stdlib.h>
        #include <tchar.h>

        /////////////////////////////////////////////////////////////////////

        int WINAPI WinMain(HINSTANCE h, HINSTANCE h2, LPSTR psz, int n) {

           // Is this the original EXE ther Clone?
           // The original EXE receives one argument,
           // the clone more.
           if (__argc == 1) {

              // Original EXE: Start the Clone.

              // Copy EXE to the temporary directory
              TCHAR szPathOrig[_MAX_PATH], szPathClone[_MAX_PATH];
              GetModuleFileName(NULL, szPathOrig, _MAX_PATH);
              GetTempPath(_MAX_PATH, szPathClone);
              GetTempFileName(szPathClone, __TEXT("Del"), 0, szPathClone);
              CopyFile(szPathOrig, szPathClone, FALSE);

              // Open the Clone using FILE_FLAG_DELETE_ON_CLOSE
              HANDLE hfile = CreateFile(szPathClone, 0, FILE_SHARE_READ, NULL,
                                   OPEN_EXISTING,FILE_FLAG_DELETE_ON_CLOSE);

              // Start the Clone. Pass the PID and the fully qualified path
              // of the original file
              TCHAR szCmdLine[512];
              HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE,
                                                GetCurrentProcessId());
              wsprintf(szCmdLine, __TEXT("%s %d \"%s\""), szPathClone,
                       hProcessOrig, szPathOrig);
              STARTUPINFO si;
              ZeroMemory(&si, sizeof(si));
              si.cb = sizeof(si);
              PROCESS_INFORMATION pi;
              CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0,
                            NULL, NULL, &si, &pi);
              CloseHandle(hProcessOrig);
              CloseHandle(hfile);

              // now the original process may terminate
              } else {

              // Clone-EXE: Delete the  original EXE after termination
              HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]);
              WaitForSingleObject(hProcessOrig, INFINITE);
              CloseHandle(hProcessOrig);
              DeleteFile(__targv[2]);
           }
           return(0);
        }

If help, thanks.

T++, Radler.
0
 
smurffCommented:
Listening
0
 
geokaAuthor Commented:
thx. i found something very interesting at the seconf link of readler, i'll check this out. du you know wheter wininit.ini is processed in win98 too ???
0
 
RadlerCommented:
I haven't tested yet.

T++, Radler.
0
 
geokaAuthor Commented:
i think both ways would work, the one of radler, i mean what i dound in the documentation, and the way  of shengw. to whom do the points belong then ???
0
 
scifibeau67Commented:
Deleting a running application is not possible. Generally, it requires a secondary application to do the actual deleting - once the original program has ceased execution.

It is possible to have Windows delete files for you at startup. But note, they are not deleted until the system is restarted.

If interested, here is the code:


----------------------
To delete "Project1.exe" on system startup you should create a c:\Windows\wininit.ini and add the following lines into it. Use full path "D:\My Projects\" instead of "D:MYPROJ~1\".


[rename]
nul=D:\My Projects\Project1.exe

0
 
lgmmCommented:
are you making a virus or something like that???
0
 
geokaAuthor Commented:
no, not a virus, that isn't hardly possible in delphi, but it's a kind of trojan, but which only works in a specific network area. so nothing dangerous or so.
i read this about wininit.ini before. radler posted the link above, so he was the first one, and he'll get the points.

and please post in comments. that really nerves rejecting answers.
0
 
RadlerCommented:
Hi llgmm and scifibeau67;

Maybe you're novice here. Welcomed to E-E. Is a great pleasure your presence with us.

T++, Radler.
0
 
geokaAuthor Commented:
as i told you, but for all the other viewers, the easyest solution is written at on of the link. the wininit.ini stuff desriped above. also huge thanx to ?, who posted the code which greats a patch. wonderful idea!!!!!!!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now