Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Killing a process...

Posted on 2006-10-27
6
Medium Priority
?
402 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
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!

 
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 400 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

926 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