pr_wainwright
asked on
Terminate Running Process
How do I terminate a running process using delphi (same as using windows task manager - End Task). Program is not a windows program so I can't use...
var
h: HWND;
begin
h := FindWindow(nil, 'The Application Caption');
if h <> 0 then PostMessage(h, WM_QUIT, 0, 0);
end;
I need to terminate using the applications name.
Thanks
Paul.
var
h: HWND;
begin
h := FindWindow(nil, 'The Application Caption');
if h <> 0 then PostMessage(h, WM_QUIT, 0, 0);
end;
I need to terminate using the applications name.
Thanks
Paul.
ASKER
esoftbg,
function KillTask(FileName:String): integer;
does not work on Windows NT (The process entry point Process32Next could not be located in the DLL kernel32.dll).
I don't know how to find the PID to use the following.
procedure CloseAProcess(PID: DWORD;);
var
h: THandle;
begin
try
h := OpenProcess(PROCESS_TERMIN ATE, false, PID);
try
if h <> 0 then TerminateProcess(h, 0);
finally
CloseHandle(h);
end;
except
ShowMessage('Error');
end;
end;
The others use FindWindow & the program is not a windows program.
Regards
Paul.
function KillTask(FileName:String):
does not work on Windows NT (The process entry point Process32Next could not be located in the DLL kernel32.dll).
I don't know how to find the PID to use the following.
procedure CloseAProcess(PID: DWORD;);
var
h: THandle;
begin
try
h := OpenProcess(PROCESS_TERMIN
try
if h <> 0 then TerminateProcess(h, 0);
finally
CloseHandle(h);
end;
except
ShowMessage('Error');
end;
end;
The others use FindWindow & the program is not a windows program.
Regards
Paul.
you have to get the handle of the window so you can get the process id. use GetWindowThreadProcessID() to find the process id.
this works:
procedure TForm1.Button1Click(Sender : TObject);
var pid: Cardinal;
ProcessHandle, wnd: THandle;
begin
wnd := FindWindow(nil, 'Audacity');
GetWindowThreadProcessID(w nd, @pid);
ProcessHandle := OpenProcess(PROCESS_TERMIN ATE, FALSE, pid);
TerminateProcess(ProcessHa ndle,4);
CloseHandle(ProcessHandle) ;
end;
this works:
procedure TForm1.Button1Click(Sender
var pid: Cardinal;
ProcessHandle, wnd: THandle;
begin
wnd := FindWindow(nil, 'Audacity');
GetWindowThreadProcessID(w
ProcessHandle := OpenProcess(PROCESS_TERMIN
TerminateProcess(ProcessHa
CloseHandle(ProcessHandle)
end;
Hi,
Here it is:
Drop a Button and Edit controls on a form. Add the code below. Write 'Project1.exe' in the Edit and press the button.
uses
TLHelp32, PsApi;
procedure CloseAProcess(PID: DWORD);
var
h: THandle;
begin
try
h := OpenProcess(PROCESS_TERMIN ATE, false, PID);
try
if h <> 0 then TerminateProcess(h, 0);
finally
CloseHandle(h);
end;
except
ShowMessage('Error');
end;
end;
function RunningProcessesList(List: TStrings): Boolean;
function BuildListTH: Boolean;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
begin
SnapProcHandle := CreateToolhelp32Snapshot(T H32CS_SNAP PROCESS, 0);
if SnapProcHandle <> THandle(-1) then
begin
Result := True;
ProcEntry.dwSize := Sizeof(ProcEntry);
NextProc := Process32First(SnapProcHan dle, ProcEntry);
while NextProc do
begin
List.AddObject(ProcEntry.s zExeFile, Pointer(ProcEntry.th32Proc essID));
NextProc := Process32Next(SnapProcHand le, ProcEntry);
end;
CloseHandle(SnapProcHandle );
end else
Result := False;
end;
function BuildListPS: Boolean;
var
PIDs: array[0..1024] of DWORD;
Handle: THandle;
Needed: DWORD;
I: Integer;
ModuleFileName: array[0..MAX_PATH] of Char;
begin
Result := EnumProcesses(@PIDs, Sizeof(PIDs), Needed);
if not Result then Exit;
for I := 0 to (Needed div Sizeof(DWORD)) - 1 do
if PIDs[I] <> 0 then
begin
Handle := OpenProcess(PROCESS_QUERY_ INFORMATIO N or PROCESS_VM_READ, False, PIDs[I]);
if Handle <> 0 then
begin
if GetModuleFileNameEx(Handle , 0, ModuleFileName, Sizeof(ModuleFileName)) = 0 then
List.AddObject('[System]', Pointer(INVALID_HANDLE_VAL UE))
else
List.AddObject(ModuleFileN ame, Pointer(PIDs[I]));
CloseHandle(Handle);
end;
end;
end;
begin
Assert(Assigned(List));
List.BeginUpdate;
try
List.Clear;
if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion = 4) then
Result := BuildListPS
else
Result := BuildListTH;
finally
List.EndUpdate;
end;
end;
procedure TForm1.Button2Click(Sender : TObject);
var
i: integer;
PID: Cardinal;
sl: TStringList;
begin
sl := TStringList.Create;
try
RunningProcessesList(sl);
repeat
i := sl.IndexOf(Edit1.Text);
if i > -1 then begin
PID := Integer(Pointer(sl.Objects [i]));
CloseAProcess(PID);
sl.Delete(i);
end;
until i = -1;
finally
sl.Free;
end;
end;
Regards, Geo
Here it is:
Drop a Button and Edit controls on a form. Add the code below. Write 'Project1.exe' in the Edit and press the button.
uses
TLHelp32, PsApi;
procedure CloseAProcess(PID: DWORD);
var
h: THandle;
begin
try
h := OpenProcess(PROCESS_TERMIN
try
if h <> 0 then TerminateProcess(h, 0);
finally
CloseHandle(h);
end;
except
ShowMessage('Error');
end;
end;
function RunningProcessesList(List:
function BuildListTH: Boolean;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
begin
SnapProcHandle := CreateToolhelp32Snapshot(T
if SnapProcHandle <> THandle(-1) then
begin
Result := True;
ProcEntry.dwSize := Sizeof(ProcEntry);
NextProc := Process32First(SnapProcHan
while NextProc do
begin
List.AddObject(ProcEntry.s
NextProc := Process32Next(SnapProcHand
end;
CloseHandle(SnapProcHandle
end else
Result := False;
end;
function BuildListPS: Boolean;
var
PIDs: array[0..1024] of DWORD;
Handle: THandle;
Needed: DWORD;
I: Integer;
ModuleFileName: array[0..MAX_PATH] of Char;
begin
Result := EnumProcesses(@PIDs, Sizeof(PIDs), Needed);
if not Result then Exit;
for I := 0 to (Needed div Sizeof(DWORD)) - 1 do
if PIDs[I] <> 0 then
begin
Handle := OpenProcess(PROCESS_QUERY_
if Handle <> 0 then
begin
if GetModuleFileNameEx(Handle
List.AddObject('[System]',
else
List.AddObject(ModuleFileN
CloseHandle(Handle);
end;
end;
end;
begin
Assert(Assigned(List));
List.BeginUpdate;
try
List.Clear;
if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion = 4) then
Result := BuildListPS
else
Result := BuildListTH;
finally
List.EndUpdate;
end;
end;
procedure TForm1.Button2Click(Sender
var
i: integer;
PID: Cardinal;
sl: TStringList;
begin
sl := TStringList.Create;
try
RunningProcessesList(sl);
repeat
i := sl.IndexOf(Edit1.Text);
if i > -1 then begin
PID := Integer(Pointer(sl.Objects
CloseAProcess(PID);
sl.Delete(i);
end;
until i = -1;
finally
sl.Free;
end;
end;
Regards, Geo
ASKER
Geo,
Still get this error using your code while running Windows NT4 (The process entry point Process32Next could not be located in the DLL kernel32.dll).
Any idea's
Thanks
Paul.
Still get this error using your code while running Windows NT4 (The process entry point Process32Next could not be located in the DLL kernel32.dll).
Any idea's
Thanks
Paul.
When are you getting that error - on startup of the app or when you are pressing the button?
The function BuildListTH should not be called if you're running NT4. The line:
if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion = 4) then
should do that.
Could you tell me what Win32Platform and Win32MajorVersion your computer reports back?
Regards, Geo
if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion = 4) then
should do that.
Could you tell me what Win32Platform and Win32MajorVersion your computer reports back?
Regards, Geo
Try:
function KillTask(ExeFileName: string): integer; //used to kill a process
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(FSnapshotHa ndle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileNam e(FProcess Entry32.sz ExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32 .szExeFile ) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(O penProcess (
PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32Proces sID), 0));
ContinueLoop := Process32Next(FSnapshotHan dle,
FProcessEntry32);
end;
CloseHandle(FSnapshotHandl e);
end;
This will kill a process by it's exe name e.g.
killtask('notepad.exe');
Regards,
Hypoviax
function KillTask(ExeFileName: string): integer; //used to kill a process
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(FSnapshotHa
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileNam
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(O
PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32Proces
ContinueLoop := Process32Next(FSnapshotHan
FProcessEntry32);
end;
CloseHandle(FSnapshotHandl
end;
This will kill a process by it's exe name e.g.
killtask('notepad.exe');
Regards,
Hypoviax
U don't need to loop through all processes to kill a specific applications if u know the window caption
var pid: Cardinal;
Process, h: THandle;
begin
wnd := FindWindow(nil, 'The Application Caption'); //get the window handle
GetWindowThreadProcessID(w nd, @pid); //then get it's pid
ProcessHandle := OpenProcess(PROCESS_TERMIN ATE, FALSE, pid); //open the process with termination rights using the PID
TerminateProcess(ProcessHa ndle,0); // terminate the process (in this case, 0 is the exit code)
CloseHandle(ProcessHandle) ; //close the process handle beacuse we don't use the process ahndle anymore
end;
This only works if you know the window caption, but not the .exe filename
StTwister
var pid: Cardinal;
Process, h: THandle;
begin
wnd := FindWindow(nil, 'The Application Caption'); //get the window handle
GetWindowThreadProcessID(w
ProcessHandle := OpenProcess(PROCESS_TERMIN
TerminateProcess(ProcessHa
CloseHandle(ProcessHandle)
end;
This only works if you know the window caption, but not the .exe filename
StTwister
ASKER
Geo,
1) The error is reported at startup
2) Windows NT 4.0 Service Pack 5 Build 1381
Regards
Paul.
1) The error is reported at startup
2) Windows NT 4.0 Service Pack 5 Build 1381
Regards
Paul.
Sorry for the late reply. I was in another city for one week.
Can you open a new project, drop one TEdit and one TButton components on a form and apply my code above? Perhaps there is something else in your current test project which causes that error.
I cannot test it here because I have no NT4 at hand anymore but I used that code on NT 4 before (Delphi 5).
Regards, Geo
Can you open a new project, drop one TEdit and one TButton components on a form and apply my code above? Perhaps there is something else in your current test project which causes that error.
I cannot test it here because I have no NT4 at hand anymore but I used that code on NT 4 before (Delphi 5).
Regards, Geo
ASKER
Geobul,
Did not call 'BuildListTH', no error when program started but pressing the button did nothing.
Reagrds
Paul.
Did not call 'BuildListTH', no error when program started but pressing the button did nothing.
Reagrds
Paul.
My fault, sorry. Modify the following lines as shown below:
in BuildListTH:
List.AddObject(UpperCase(P rocEntry.s zExeFile), Pointer(ProcEntry.th32Proc essID));
in BuildListPS:
List.AddObject(UpperCase(E xtractFile Name(Modul eFileName) ), Pointer(PIDs[I]));
in ButtonClick:
i := sl.IndexOf(UpperCase(Edit1 .Text));
and try again.
Regards, Geo
in BuildListTH:
List.AddObject(UpperCase(P
in BuildListPS:
List.AddObject(UpperCase(E
in ButtonClick:
i := sl.IndexOf(UpperCase(Edit1
and try again.
Regards, Geo
ASKER
Geo,
Get this error using your new code while running Windows NT4 (The process entry point Process32First could not be located in the DLL kernel32.dll).
Thanks
Paul.
Get this error using your new code while running Windows NT4 (The process entry point Process32First could not be located in the DLL kernel32.dll).
Thanks
Paul.
Process32First and the others seem not to work in NT4. You will need to find another way to enumerate processes before you can terminate them.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Geo,
The company where I work are rolling out XP soon & I know your initial code works ok so I will award you the points.
Thanks
Paul.
The company where I work are rolling out XP soon & I know your initial code works ok so I will award you the points.
Thanks
Paul.
Paul, thanks for the points. I'm so sorry I wasn't able to help you more.
Regards, Geo
Regards, Geo
https://www.experts-exchange.com/questions/21312881/terminate-all-open-applications.html
https://www.experts-exchange.com/questions/20758535/End-running-process.html
https://www.experts-exchange.com/questions/20651898/How-To-Kill-A-Process-Running-in-Windows.html