Two ways to force Process Termination: C # And C ++
Recently, I was working on a large project. Due to some problems related to the association between processes, in short, we had to force some processes to stop to ensure the normal operation of the system. I found it online, I did not find a detailed introduction and a comprehensive article in this regard, so I wrote two methods according to the msdn instructions, hoping to help my friends who encountered similar problems.
First, let's take a look at the implementation method of C #. Since C # encapsulates processes well, we only need a small number of statements to complete this work, it is worth noting that the parameter validity detection and name comparison are important because many unexpected processes may run in the system. Therefore, when comparing a name, you must use an equal value instead of an inclusive or comparative start and end, otherwise, the error process may be killed. The related code is as follows.
C # code:
// Return true for successful and false for failed
Public bool findandkillprocpolicyname (string name)
{
// Parameter check
If (0 = Name. length)
{
Return
False;
}
// Find the named process and terminate it
Foreach (process winproc
In process. getprocesses ())
{
// Use equals for the task in case we kill
// A wrong Process
If (winproc. processname. Equals (name ))
{
Winproc. Kill ();
Return
True;
}
}
Return false;
}
Using C ++ to do the same job requires a few more lines of code. First, you need to take a snapshot of all processes in the current system. The function to complete this job is createconlhelp32snapshot (), in the header file # include <tlhelp32.h>, after calling this function and returning a valid handle, we need to find the process we want to end in this snapshot. It is worth mentioning that process32first (), the logic of the process32next () functions should be carefully read by the Code. Otherwise, an error may be caused by missing a process. In addition, many articles on OpenProcess () online say that sedebug is required to open a process, actually, this is not required, as long as we pass process_terminate
As a parameter, you can successfully open the process.
C ++ code:
Bool findandkillprocpolicyname (lpctstr strprocessname)
{
If (null = strprocessname)
{
Return false;
}
Handle handle32snapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (invalid_handle_value = handle32snapshot)
{
Return false;
}
Processentry32 pentry;
Pentry. dwsize = sizeof (processentry32 );
// Search for all the process and terminate it
If (process32first (handle32snapshot, & pentry ))
{
Bool bfound = false;
If (! _ Tcsicmp (pentry. szexefile, strprocessname ))
{
Bfound = true;
}
While ((! Bfound) & process32next (handle32snapshot, & pentry ))
{
If (! _ Tcsicmp (pentry. szexefile, strprocessname ))
{
Bfound = true;
}
}
If (bfound)
{
Closehandle (handle32snapshot );
Handle handle = OpenProcess (process_terminate, false, pentry. th32processid );
Bool bresult = terminateprocess (handle, 0 );
Return bresult;
}
}
Closehandle (handle32snapshot );
Return false;
}
Summary
This article takes C ++ and C # as examples to introduce two methods of force Process Termination. After strict tests, the above Code can successfully run and complete related tasks, however, it is worth mentioning that this method of ending the process is the best practice. If there are other solutions, try not to end the process in this way, because it may cause the relevant memory or some resources to be released, it is recommended to use this method only when the program is installed or upgraded.