turk_bayr.jpg (8863 bytes)

header.jpg (23201 bytes)

PROG
RAM

22 Haz 2001 KILL PROCESS BY NAME

VC++ Articels


button_programming.jpg (4736 bytes)






 

(works in: Microsoft Windows 95/98/ME/NT/2000)

It is sometimes necessary to terminate a running process in MS-Windows. The Win32 API provides the TerminateProcess function for this, but it requires a "handle to the process". In many cases it is not apparent how to get this handle if all you know is the process name. The following routine lets you get around this problem.

The routine is named KILL_PROC_BY_NAME, and is available as part of the C++ source file EXAM28.CPP. The routine has the following calling parameters:

        int KILL_PROC_BY_NAME(char *)

where the process name is supplied as a zero-terminated C char string and it returns an integer completion code.

You can download the file in ZIP format by clicking on the following link.

Download Exam28.zip (3 kB, last modified: 12/29/2000)

This works for Microsoft Windows 95/98/ME/NT/2000. I have tested with Visual C++ v6.0 but it should work with other versions also. Please let me know of exceptions.

After downloading, unzip the file Exam28.cpp into any folder. It contains a short console mode program which demonstrates how to call KILL_PROC_BY_NAME to terminate the NotePad program.

Note: This routine uses the Windows API function TerminateProcess to terminate the process. This may not be suitable for all purposes. In particular, note the following remarks from the MSDN description of TerminateProcess.

 Remarks (on TerminateProcess) 
 The TerminateProcess function is used to unconditionally cause a process  to exit. Use it only in extreme circumstances. The state of global data  maintained by dynamic-link libraries (DLLs) may be compromised if  TerminateProcess is used rather than ExitProcess.   TerminateProcess causes all threads within a process to terminate,  and causes a process to exit, but DLLs attached to the process are not notified that the process is terminating.  Terminating a process causes the following:
     1. All of the object handles opened by the process are closed.
    2. All of the threads in the process terminate their execution.
    3. The state of the process object becomes signaled, satisfying any threads that had been waiting for the process to terminate.
     4. The states of all threads of the process become signaled, satisfying any threads that had been waiting for the threads to terminate.
     5. The termination status of the process changes from STILL_ACTIVE to the exit value of the process.

 Terminating a process does not cause child processes to be terminated.
 Terminating a process does not necessarily remove the process object  from the system. A process object is deleted when the last handle to the  process is closed.

 

 

home | about me | links | projects | clean energy | programming

 

 

 

 

 

 

Hosted by www.Geocities.ws

1