This is from an old Delphi program (win32), it will list all running processes, with their filenames.
Maybe you can use this as a startingpoint.
It uses TlHlp32 unit.
procedure TForm1.SnapBtnClick(Sender: TObject);
var H: THandle;
PE: TProcessEntry32;
GotModule: Boolean;
ME: TModuleEntry32;
begin
Memo1.Clear;
with Memo1.SelAttributes do
begin
Color := clRed;
Style := Style + [fsBold];
end;
Memo1.Lines.Add(Format('%-10s %-10s %-10s %-8s %s',
['ProcessID','ModuleID','ParentID','Threads','Naam']));
Memo1.SelAttributes := Memo1.Defattributes;
try
H := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if H = 0 then Exit;
PE.dwSize := SizeOf(TProcessEntry32);
if Process32First(H,PE) then
repeat
Memo1.Lines.Add(Format('%-10x %-10x %-10x %-8x %s',
[PE.th32ProcessID,PE.th32ModuleID,PE.th32ParentProcessID,
PE.cntThreads,PE.szExeFile]));
until not Process32Next(H,PE);
finally
CloseHandle(H);
end;
end;
Once you've got the processID you can terminate (kill, really, the process will not be able to save data) the process like this:
Try
//trying to get access to process
AHandle := OpenProcess(PROCESS_ALL_ACCESS,False,ID); //uses windows
if AHandle = 0 then
begin
//cannot open process
exit;
end;
//try to kill th eprocess (in a nasty way)
if not TerminateProcess(AHandle,255) then
begin
//failure to kill
exit;
end;
Finally
CloseHandle(AHandle);
end;
Or (probably nicer, send a WM_QUIT message to the process(?), never done that though.
Be aware, this code might not function in accectly the way it does in NT based Windows (w2K, XP and above) as it does in win9x (you might not be able to get access to the process, because you don't have the proper access-rights).
Bart