CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    killing a process according to its PID

    Hi,

    I'm looking for an API which enable killing a process according to its process ID.

    Is there an API like that, I didn't find it
    If not how can I kill a process, if I have its name (xxx.exe) & ID I can have 2 processes with the same name - running the same application twice (of course different PID)

    Thanks in advance
    Avi123

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: killing a process according to its PID

    Use OpenProcess() with X_TERMINATE rights and then use TerminateProcess() to hardcode terminate a process. Or try to send WM_QUIT firstly, this is a finer and neater solution than just killing it.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: killing a process according to its PID

    Quote Originally Posted by NoHero
    Use OpenProcess() with X_TERMINATE rights and then use TerminateProcess() to hardcode terminate a process. Or try to send WM_QUIT firstly, this is a finer and neater solution than just killing it.
    Before using 'TerminateProcess()' (which should only be used as a last resort) you might want to take a look at the following FAQ which demonstrates how to shutdown a process "cleanly"...

  4. #4
    Join Date
    Sep 2003
    Posts
    815

    Re: killing a process according to its PID

    I know about all the other ways I can kill a process
    and I would like to kill it according to the PID, is it possible?
    If not how do I kill it if all I have is the name (xxx.exe) & the pid

    Thanks
    Avi123

  5. #5
    Join Date
    May 2005
    Posts
    4,954

    Re: killing a process according to its PID

    Quote Originally Posted by avi123
    I know about all the other ways I can kill a process
    and I would like to kill it according to the PID, is it possible?
    If not how do I kill it if all I have is the name (xxx.exe) & the pid

    Thanks
    Avi123
    like NoHero mentioned above Correctly, you can use the ::OpenProcess with the PID you have, the ::OpenProcess will return you a Handle to the Process and then you can call ::TerminateProcess() with the Handle
    Code:
    HANDLE hP= ::OpenProcess(PROCESS_ALL_ACCESS,0,Pid/*the pid you have*/);
    ::TerminateProcess( hP,0);
    another method you can try using is locating the HWND of the process from the Pid, and then ::SendMessage(, WM_COSE,, ); to the window.

    Code:
    HWND h = ::GetTopWindow(0 )
    
    while ( h )
    {
             DWORD pid;
             DOWRD dwTheardId = ::GetWindowThreadProcessId( h,&pid);
             if ( pid == /*your process that you looking for*/
             {
                 /*here you have the thread id*/
                ::SendMessage(h,WM_CLOSE,0,0);
                 break;
              }
             h = ::GetNextWindow( h , GW_HWNDNEXT);
    }
    Cheers
    Last edited by golanshahar; January 1st, 2009 at 12:41 AM.

  6. #6
    Join Date
    Sep 2003
    Posts
    815

    Re: killing a process according to its PID

    Thank you all very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured