Solved

Killing a process...

Posted on 2006-10-27
6
389 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:ciuly
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:ciuly
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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:
ciuly 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now