Solved

how to delete itself

Posted on 2000-04-25
18
389 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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