Kill a process by process name

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.