Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Killing a process...

Posted on 2006-10-27
6
Medium Priority
?
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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