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

x
?
Solved

Kill a process with the pid.

Posted on 1999-01-28
3
Medium Priority
?
731 Views
Last Modified: 2008-02-01
I know the pid of a process that I want to kill.  How I can do it on Windows NT? I haven´t found the kill function or its equivalent.
0
Comment
Question by:riclueje
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
_Scotch_ earned 200 total points
ID: 1258473
If the target process is cooperative you can send it a message
like WM_QUIT or something.  Otherwise I'd look at:

OpenProcess()
TerminateProcess()
CloseProcess()
0
 

Author Comment

by:riclueje
ID: 1258474
Thanks, but I only know the process's pid.  I use de spawnl function that return the process's pid and I need to kill the process started with the spawnl funtion.  
0
 
LVL 3

Expert Comment

by:_Scotch_
ID: 1258475
Right... You do it this way:

HANDLE aHandle = OpenProcess(PROCESS_TERMINATE, FALSE, proc-id);
int aStatus = TerminateProcess(aHandle, desired-exit-code);
if (!aStatus)  {
    char s[80];
    sprintf(s, "TerminateProcess() failed, code: %d", aStatus);
    AfxMessageBox(s);
    }
CloseHandle(aHandle);

Sorry about the typo before... CloseHandle() should be used to
free the handle, not CloseProcess()...
0

Featured Post

Independent Software Vendors: 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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

916 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