Terminates the executable (EXE) program, first traversing the process, finding the process ID number for the process name, and then terminating the process based on the process ID.
Sample code: Includes traversing the process code, and closing the code.
Code:
* * * main.cpp * * Created on:2014.06.08 * author:spike//*vs 2012*/#include <iostre
am> #include <string> #include <map> #include <windows.h> #include <TlHelp32.h>
using namespace Std;
BOOL Traverseprocesses (std::map<std::string, int>& _nameid) {PROCESSENTRY32 pe32;
pe32.dwsize = sizeof (PE32);
HANDLE Hprocesssnap = createtoolhelp32snapshot (th32cs_snapprocess, 0); if (Hprocesssnap = = Invalid_handle_value) {std::cout << "createtoolhelp32snapshot error!" << std::en
DL;;
return false;
BOOL bresult =process32first (Hprocesssnap, &pe32);
int num (0);
while (bresult) {std::string name = Pe32.szexefile;
int id = PE32.TH32PROCESSID; Std::cout << "[<< ++num <<"]: "<<" Process Name: "//More Highlights: HTTP://WWw.bianceng.cnhttp://www.bianceng.cn/programming/cplus///<< name << "" << "ProcessID:" <& Lt
id<< Std::endl; _nameid.insert (std::p air<string, int> (name, id));
Dictionary Storage bresult = Process32Next (HPROCESSSNAP,&PE32);
} closehandle (HPROCESSSNAP);
return true;
BOOL TerminateProcess (const std::string _name) {DWORD dwid;
Std::map<std::string, int> NameID;
if (!traverseprocesses (NameID)) {//variable process cout << "Print processes error!" << Endl;
}/*printf ("Please enter the name of a process to Terminal:");
std::string name;
cin >> name;*/dwid = Nameid[_name];
BOOL bRet = FALSE;
HANDLE hprocess = OpenProcess (Process_terminate, FALSE, Dwid);
if (hprocess!= NULL) BRet = terminateprocess (hprocess, 0);
CloseHandle (hprocess); if (bRet) {std::cout << "Terminate Process success!" << Std::endl;
else {std::cout << "Terminate Process error!" << Std::endl;
return false;
return true;
int main () {const std::string program ("Image.exe");
if (!terminateprocess (program)) {cout << "Terminate Process error!" << Endl;
return 0; }
Note: Image.exe is the program that launches the display picture, when execution completes, Image.exe closes.
Author: csdn Blog spike_king