Traverse all processes and find the specified process to stop

Source: Internet
Author: User

[Cpp]
// Testlog. cpp: defines the entry point of the console application.
//
 
# Include "stdafx. h"
# Include <iostream>
# Include <Windows. h>
# Include <TlHelp32.h>
 
Using namespace std;
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
// HMODULE Mymhandle;
// _ Tsystem (L "calc.exe ");
// Mymhandle = GetModuleHandle (0 );
// TerminateProcess (handle1, 0 );
//
// Int RetValue = GetLastError ();
 
 
TCHAR c [] = {L "cmd.exe"}; // defines the string and initializes it. c is an 8-length string ending with '\ 0' and defines a character as 'x ',
 
HANDLE handle; // defines the snapshot HANDLE of the createconlhelp32snapshot system.
 
HANDLE handle1; // defines the process HANDLE to end
 
Handle = createconlhelp32snapshot (TH32CS_SNAPPROCESS, 0); // obtain the System Snapshot handle
 
PROCESSENTRY32 * info; // define the PROCESSENTRY32 Structure
 
// Set the dwSize Member of the PROCESSENTRY32 structure to sizeof (PROCESSENTRY32)
 
Info = new PROCESSENTRY32;
 
Info-> dwSize = sizeof (PROCESSENTRY32 );
 
// Call the Process32First function to obtain the process list from the snapshot.
 
Process32First (handle, info );
 
// Call Process32Next repeatedly until the function returns FALSE.
 
While (Process32Next (handle, info )! = FALSE)
{
 
Info-> szExeFile; // point to the process name
 
 
// The strcmp string comparison function must end with the same
 
 
If (wcscmp (c, info-> szExeFile) = 0)
 
 
 
{
// MessageBox (NULL, info-> szExeFile, TEXT ("HelloMsg"), 0 );
 
// PROCESS_TERMINATE indicates that the end operation is enabled, FALSE = can be inherited, info-> th32ProcessID = process ID
 
Handle1 = OpenProcess (PROCESS_TERMINATE, FALSE, info-> th32ProcessID );
 
// End the process
 
TerminateProcess (handle1, 0 );
 
 
}
 
 
}
CloseHandle (handle );
 
 
 
 
 
Return 0;
}

// Testlog. cpp: defines the entry point of the console application.
//

# Include "stdafx. h"
# Include <iostream>
# Include <Windows. h>
# Include <TlHelp32.h>

Using namespace std;

Int _ tmain (int argc, _ TCHAR * argv [])
{
// HMODULE Mymhandle;
// _ Tsystem (L "calc.exe ");
// Mymhandle = GetModuleHandle (0 );
// TerminateProcess (handle1, 0 );
//
// Int RetValue = GetLastError ();


TCHAR c [] = {L "cmd.exe"}; // defines the string and initializes it. c is an 8-length string ending with '\ 0' and defines a character as 'x ',

HANDLE handle; // defines the snapshot HANDLE of the createconlhelp32snapshot system.

HANDLE handle1; // defines the process HANDLE to end

Handle = createconlhelp32snapshot (TH32CS_SNAPPROCESS, 0); // obtain the System Snapshot handle

PROCESSENTRY32 * info; // define the PROCESSENTRY32 Structure

// Set the dwSize Member of the PROCESSENTRY32 structure to sizeof (PROCESSENTRY32)

Info = new PROCESSENTRY32;

Info-> dwSize = sizeof (PROCESSENTRY32 );

// Call the Process32First function to obtain the process list from the snapshot.

Process32First (handle, info );

// Call Process32Next repeatedly until the function returns FALSE.

While (Process32Next (handle, info )! = FALSE)
{

Info-> szExeFile; // point to the process name


// The strcmp string comparison function must end with the same


If (wcscmp (c, info-> szExeFile) = 0)

 

{
// MessageBox (NULL, info-> szExeFile, TEXT ("HelloMsg"), 0 );

// PROCESS_TERMINATE indicates that the end operation is enabled, FALSE = can be inherited, info-> th32ProcessID = process ID

Handle1 = OpenProcess (PROCESS_TERMINATE, FALSE, info-> th32ProcessID );

// End the process

TerminateProcess (handle1, 0 );


}


}
CloseHandle (handle );

 

 

Return 0;
}

 

Related Article

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.