BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to kill a process

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Deployment
View previous topic :: View next topic  
Author Message
Juan Araya
Guest





PostPosted: Thu May 19, 2005 9:37 pm    Post subject: How to kill a process Reply with quote




Good morning

How can I kill or terminate inmediatly a process that is running and appears in the Microsoft task bar using Delphi 5.

Thanks a lot in advanced for your help

Juan Araya
Back to top
Bill Todd
Guest





PostPosted: Thu May 19, 2005 11:45 pm    Post subject: Re: How to kill a process Reply with quote



You will have better luck with your question in the nativeapi newsgroup
where the Windows API wizards congregate.

--
Bill Todd (TeamB)


Juan Araya wrote:

Quote:

Good morning

How can I kill or terminate inmediatly a process that is running and
appears in the Microsoft task bar using Delphi 5.

Thanks a lot in advanced for your help

Juan Araya

Back to top
Julio C. Saucedo Monteneg
Guest





PostPosted: Sun May 22, 2005 3:31 pm    Post subject: Re: How to kill a process Reply with quote




Autor: Misha Moellner
Homepage: http://www.delphi3000.com


{For Windows 9x/ME/2000/XP }

uses
Tlhelp32;

function 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;

KillTask('notepad.exe');


{ For Windows NT/2000/XP }

procedure KillProcess(hWindowHandle: HWND);
var
hprocessID: INTEGER;
processHandle: THandle;
DWResult: DWORD;
begin
SendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0,
SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);

if isWindow(hWindowHandle) then
begin
// PostMessage(hWindowHandle, WM_QUIT, 0, 0);

{ Get the process identifier for the window}
GetWindowThreadProcessID(hWindowHandle, @hprocessID);
if hprocessID <> 0 then
begin
{ Get the process handle }
processHandle := OpenProcess(PROCESS_TERMINATE or
PROCESS_QUERY_INFORMATION,
False, hprocessID);
if processHandle <> 0 then
begin
{ Terminate the process }
TerminateProcess(processHandle, 0);
CloseHandle(ProcessHandle);
end;
end;
end;
end;

KillProcess(FindWindow('notepad',nil));
Back to top
Ben Hochstrasser
Guest





PostPosted: Sun May 22, 2005 4:10 pm    Post subject: Re: How to kill a process Reply with quote

Julio C. Saucedo Montenegro wrote:

Quote:
Autor: Misha Moellner

<snip code>

Be aware that this code unconditionally kills a process. No way of
"gracefully" terminating the application. You may want to find out the
application's main window and send it a WM_QUIT (?) message before
resorting to such "brutal" methods.

--
Ben

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Deployment All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.