Solved

delete file from task manager and also WINDOWS

Posted on 2009-07-09
9
249 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 36

Expert Comment

by:Geert Gruwez
ID: 24820489
way behind in what ?
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 24820491
of rewriting task manager ?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

17 Experts available now in Live!

Get 1:1 Help Now