Solved

how to delete itself

Posted on 2000-04-25
18
387 Views
Last Modified: 2011-04-14
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
Comment
Question by:geoka
  • 5
  • 3
  • 3
  • +6
18 Comments
 

Expert Comment

by:lgmm
ID: 2748818
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
 
LVL 3

Expert Comment

by:ckaneta
ID: 2748854
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
 
LVL 3

Expert Comment

by:shenqw
ID: 2750289
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Expert Comment

by:shenqw
ID: 2750290
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
 
LVL 1

Expert Comment

by:veeramani
ID: 2750823
Hi,
   press ctrl+alt+del
 then end task
then press end task
finish
regards,
veeramani
0
 

Author Comment

by:geoka
ID: 2752595
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
 
LVL 3

Expert Comment

by:shenqw
ID: 2752834
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 2753252
You can insert a regsitry key in the \Software\Microsoft\Windows\CurrenVersion\RunOnce that deletes your app.
0
 
LVL 4

Accepted Solution

by:
Radler earned 50 total points
ID: 2753442
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
 
LVL 3

Expert Comment

by:smurff
ID: 2754939
Listening
0
 

Author Comment

by:geoka
ID: 2755873
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
 
LVL 4

Expert Comment

by:Radler
ID: 2757543
I haven't tested yet.

T++, Radler.
0
 

Author Comment

by:geoka
ID: 2758569
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
 

Expert Comment

by:scifibeau67
ID: 2785819
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
 

Expert Comment

by:lgmm
ID: 2788767
are you making a virus or something like that???
0
 

Author Comment

by:geoka
ID: 2788907
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
 
LVL 4

Expert Comment

by:Radler
ID: 2793057
Hi llgmm and scifibeau67;

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

T++, Radler.
0
 

Author Comment

by:geoka
ID: 2794214
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 215
add combobox item based on numbers 9 141
Delphi application Soap connection 5 103
Find and Replace Stream with 0s 8 62
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question