Solved

delete file from task manager and also WINDOWS

Posted on 2009-07-09
9
256 Views
Last Modified: 2013-11-23
I need to have me type in edit9.text a name that i know exists in the taskmanager. Then to click button 1 to delete it. However, when it deleted example test.exe to also read the info in edit9.text and delete that same file in windows folder, AFTER its been killed.

500 points  for full script pls im freakin way behind.
0
Comment
Question by:SonicM3
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:ccupo
ID: 24819374

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Tlhelp32;
 
type
  TForm1 = class(TForm)
    Edit4: TEdit;
    Button1: TButton;
    function KillTask(ExeFileName: string): Integer;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
function TForm1.KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  while Integer(ContinueLoop) <> 0 do  begin
    if (
         (UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or
         (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))
        ) then
      Result :=
        Integer(
          TerminateProcess(
            OpenProcess(
              PROCESS_TERMINATE,
              BOOL(0),
              FProcessEntry32.th32ProcessID
            ),
            0
          )
        );
     ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
KillTask(Edit4.Text); // :)
end;
 
end.

Open in new window

0
 

Author Comment

by:SonicM3
ID: 24819496
thankyou friend, it kills the task, but it doesnt delete it from the windows folder. After the task has been killed it then has to read the text inside the edit4.text again and delete that filename in the WINDOWS folder.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24820489
way behind in what ?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24820491
of rewriting task manager ?
0
 

Author Comment

by:SonicM3
ID: 24822205
lads?  any help yet cheers.
0
 
LVL 1

Expert Comment

by:ccupo
ID: 24822273
unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Tlhelp32, PsAPI;

type
    TForm1 = class(TForm)
        Edit4: TEdit;
        Button1: TButton;
        function KillTask(ExeFileName: string): integer;
        function ProcessFileName(PID: DWORD): string;
        // stdcall external 'Psapi.dll';

        // function GetProcessFullPathFileName(PID: DWORD): string;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

var
    Form1: TForm1;
    full_filename: string;

implementation

{$R *.dfm}

function TForm1.ProcessFileName(PID: DWORD): string;
var
    Handle: THandle;
begin
    Result := '';
    Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
    if Handle <> 0 then
        try
            SetLength(Result, MAX_PATH);
            begin
                if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
                    SetLength(Result, StrLen(PChar(Result)))
                else
                    Result := '';
            end;

        finally
            CloseHandle(Handle);
        end;
end;

function TForm1.KillTask(ExeFileName: string): integer;
const
    PROCESS_TERMINATE = $0001;
var
    ContinueLoop: BOOL;
    FSnapshotHandle: THandle;
    FProcessEntry32: TProcessEntry32;
begin
    Result := 0;
    FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
    ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
    while integer(ContinueLoop) <> 0 do
    begin
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
            or
            (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))
            ) then
        begin
            full_filename := ProcessFileName(FProcessEntry32.th32ProcessID);
            Result :=
                integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,
                BOOL(0),
                FProcessEntry32.th32ProcessID
                ),
                0
                )
                );
        end;
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
    end;
    CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    KillTask(Edit4.Text);
    DeleteFile(full_filename);
end;




end.
0
 
LVL 1

Accepted Solution

by:
ccupo earned 500 total points
ID: 24822284
sorry, take Code Snippet:
unit Unit1;
 
interface
 
uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Tlhelp32, PsAPI;
 
type
    TForm1 = class(TForm)
        Edit4: TEdit;
        Button1: TButton;
        function KillTask(ExeFileName: string): integer;
        function ProcessFileName(PID: DWORD): string;
        // stdcall external 'Psapi.dll';
 
        // function GetProcessFullPathFileName(PID: DWORD): string;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;
 
var
    Form1: TForm1;
    full_filename: string;
 
implementation
 
{$R *.dfm}
 
function TForm1.ProcessFileName(PID: DWORD): string;
var
    Handle: THandle;
begin
    Result := '';
    Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
    if Handle <> 0 then
        try
            SetLength(Result, MAX_PATH);
            begin
                if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
                    SetLength(Result, StrLen(PChar(Result)))
                else
                    Result := '';
            end;
 
        finally
            CloseHandle(Handle);
        end;
end;
 
function TForm1.KillTask(ExeFileName: string): integer;
const
    PROCESS_TERMINATE = $0001;
var
    ContinueLoop: BOOL;
    FSnapshotHandle: THandle;
    FProcessEntry32: TProcessEntry32;
begin
    Result := 0;
    FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
    ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
    while integer(ContinueLoop) <> 0 do 
    begin
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
            or
            (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))
            ) then
        begin
            full_filename := ProcessFileName(FProcessEntry32.th32ProcessID);
            Result :=
                integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,
                BOOL(0),
                FProcessEntry32.th32ProcessID
                ),
                0
                )
                );
        end;
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
    end;
    CloseHandle(FSnapshotHandle);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
    KillTask(Edit4.Text);
    DeleteFile(full_filename);
end;
 
 
 
 
end.

Open in new window

0
 

Author Comment

by:SonicM3
ID: 24824156
Thankyou, and i just wish i could give you more points for your hard work.
0
 

Author Closing Comment

by:SonicM3
ID: 31601882
excellent
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

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 108
Downloading email attachments 2 79
Graphics32 under Delphi 10.1 Berlin 2 98
DBCtrlGrid, Delphi, Scroll 8 37
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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
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…

808 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