Solved

delete file from task manager and also WINDOWS

Posted on 2009-07-09
9
253 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now