Solved

delete file from task manager and also WINDOWS

Posted on 2009-07-09
9
250 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
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24820491
of rewriting task manager ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

16 Experts available now in Live!

Get 1:1 Help Now