Solved

Killing a process...

Posted on 2006-10-27
6
395 Views
Last Modified: 2010-04-05
Hi guys,

I want to kill a running process... I am using delphi 7 and windows xp sp2... How I can do that?

regards
Lopem
0
Comment
Question by:lopem
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17820999
you will use TerminateProcess(<PID>,<exit code>); if you don't know the PID, you can find it by listing all processes and finding the right one by name: http://delphi.about.com/od/windowsshellapi/l/aa080304a.htm
0
 
LVL 3

Author Comment

by:lopem
ID: 17821066
any code to share, please?
regards
Lopem
0
 
LVL 28

Expert Comment

by:2266180
ID: 17821086
well the code is already there on delphi.about.com. you didn't give any specific information so I cannot make a code that will work for anything. if you have the pid of the process then you call terminateprocess on that pid and you're done. if you don't have the pid, then you must search somehow for that application. there are more ways of doing depending on what information you have about that process (executabl path, window title, etc. all are done differently). I gave you a solution for searching through names. if that is nbot the information you hold., then you must tell us what you do know about the process you want to kill.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:lopem
ID: 17821117
Hi again, Ciuly, thanks for your rapid answer. I need to kill msn messenger only if it is loaded... I know if the msn messenger is active using:

function ActiveCaption: string;
var
  Len: LongInt;
  Title: string;
  Handle : THandle;
begin
  Result := '';
  Handle := GetForegroundWindow;
  if Handle <> 0 then
  begin
    Len := GetWindowTextLength(Handle) + 1;
    SetLength(Title, Len);
    GetWindowText(Handle, PChar(Title), Len);
    ActiveCaption := TrimRight(Title);
  end;
end;

and calling this function this way:

  Label1.Caption := ActiveCaption;
   if Pos('MSN Messenger', Label1.Caption)<> 0 then
   begin
     //I want here to put the code to kill the msn messenger...
   end;

regards
Manuel Lopez (lopem)
0
 
LVL 28

Accepted Solution

by:
2266180 earned 100 total points
ID: 17821251
well, having the title, and using the code I already given link to, it would translate to modifying the enumprocess function to something like this:

function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  //if the returned value in null the
  //callback has failed, so set to false and exit.
  if (hHwnd=NULL) then
  begin
    result := false;
  end
  else
  begin
    //additional functions to get more
    //information about a process.
    //get the Process Identification number.
    GetWindowThreadProcessId(hHwnd,pPid);
    SetLength(title, 255);
    //get the process title; usually displayed
    //on the top bar in visible process
    SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
    //Display the process information
    //by adding it to a list box
    ProcessForm.ProcessListBox.Items.Add
      (title + ' '+ IntToStr(pPid));
    Result := true;
  end;
end;

(you can load this in a tstringlist instead of a listbox, thogh for debugging pursposes you might want to leave it in a listbox for starters.)

and then in your code doiing something like this:

   if Pos('MSN Messenger', Label1.Caption)<> 0 then
   begin
     i:=ProcessListbox.items.idnexof(Label1.Caption);// var i:integer;
     assert(i>-1);// if this fails you have some problems
     pid:='';// var pid:string;
     s:=ProcessListbox[i];//var s:string;
     while s[length(s)] in ['0'..'9'] do
     begin
       pid:=s[length(s)]+pid;
       delete(s,length(s),1);
     end;
     terminateprocess(strtoint(pid),0);
   end;

not tested but should work.
0
 
LVL 3

Author Comment

by:lopem
ID: 17821297
let me try! thanks man.
regards
Lopem
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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