Shut down a process based on the process name. The core method is to use a snapshot to obtain the process id based on the process name, and then kill the process.
The implementation is as follows:
# Include "tlhelp32.h"
// Function: scan the process
// Parameter: process name
// Return process ID
DWORD ctcpserver: scanprocess (tchar * processname)
{
DWORD processid = 0; // store the ID of the search process
Handle hhandle = createconlhelp32snapshot (th32cs_snapprocess, 0); // create a snapshot for the current system process
DWORD dwid =: getcurrentprocessid (); // ID of the current process
If (invalid_handle_value! = Hhandle) // If the snapshot is created successfully
{
Processentry32 stentry;
Stentry. dwsize = sizeof (processentry32 );
If (process32first (hhandle, & stentry ))
{
Do
{
If (strstr (stentry. szexefile, processname) // compare the process name
{
If (dwid! = Stentry. th32processid)
{
Processid = stentry. th32processid;
Break;
}
}
} While (process32next (hhandle, & stentry); // find the next process in the snapshot
}
Closehandle (hhandle); // release the snapshot handle.
}
Return processid;
}
Call example:
DWORD processid = scanprocess ("*. EXE ");
Handle hprocess = OpenProcess (process_terminate, false, processid );
Terminateprocess (hprocess, 0 );
In this way, you can find the process name to kill the process!