Class code for enabling/disabling/traversing programs (C ++)
Class contains four functions,Start the program, traverse all processes, close the program, and traverse the dynamic link library on which the process depends.
Example: Image.exe is a pre-generated executable program (exe). It starts the program and closes the program at an interval of 5 seconds.
Use methods to participate in the test program.
Code:
/** Process. h ** Created on: 2014.06.08 * Author: Spike * // * vs 2012 */# ifndef TRAVERSEPROCESSMODEL_H # define TRAVERSEPROCESSMODEL_H # include
# Include
# Include
# Include
# Include
# Include
// Snapshotclass Process {public: bool startProcess (const std: string _ name); bool terminateProcess (const std: string _ name); bool traverseModels (const std :: string _ name); bool traverseProcesses (std: map
& _ NameID) ;};# endif // TRAVERSEPROCESSMODEL_H/** process. cpp ** Created on: 2014.06.08 * Author: Spike * // * V. S. 2012 */# include process. husing namespace std; bool Process: startProcess (const std: string name) {STARTUPINFO si; // set memset (& si, 0, sizeof (STARTUPINFO); si. cb = sizeof (STARTUPINFO); si. dwFlags = STARTF_USESHOWWINDOW; si. wShowWindow = SW_SHOW; PROCESS_INFORMATION pi; // parameter end/* printf (Please en Ter the name of process to start :); std: string name; cin> name; */if (! CreateProcessA (NULL, (LPSTR) name. c_str (), NULL, NULL, FALSE, 0, NULL, NULL, & si, & pi) {cout <
& _ NameID) {PROCESSENTRY32 pe32; pe32.dwSize = sizeof (pe32); HANDLE hProcessSnap = creatw.lhelp32snapshot (TH32CS_SNAPPROCESS, 0); if (hProcessSnap = signature) {std :: cout <createconlhelp32snapshot Error! <Std: endl; return false;} BOOL bResult = Process32First (hProcessSnap, & pe32); while (bResult) {std: string name = pe32.szExeFile; int id = pe32.th32ProcessID; // std: cout <Process Name: <name <ProcessID: <id <std: endl; _ nameID. insert (std: pair
(Name, id); // dictionary storage bResult = Process32Next (hProcessSnap, & pe32);} CloseHandle (hProcessSnap); return true;} bool Process: terminateProcess (const std:: string _ name) {std: map
NameID; if (! TraverseProcesses (nameID) {cout <Traverse Processes Error! <Endl;} DWORD dwId;/* printf (Please enter the name of 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;} bool Process: traverseModels (const std: string _ name) {std: map
NameID; if (! TraverseProcesses (nameID) {cout <Traverse Processes Error! <Endl;} DWORD dwId;/* printf (Please enter the name of process to traverse processmodels :); std: string name; cin> name; */dwId = nameID [_ name]; HANDLE hModuleSnap = createconlhelp32snapshot (TH32CS_SNAPMODULE, dwId); if (hModuleSnap = INVALID_HANDLE_VALUE) {printf (outputs! ); Return false;} MODULEENTRY32 module32; module32.dwSize = sizeof (module32); BOOL bResult = Module32First (hModuleSnap, & module32); while (bResult) {std: wcout <Module: <std: left <std: setw (25) <module32.szModule <endl <Path: <module32.szExePath <std: endl; bResult = Module32Next (hModuleSnap, & module32);} CloseHandle (hModuleSnap); return true ;}
Test code:
/* * main.cpp * * Created on: 2014.06.08 * Author: Spike *//*vs 2012*/#include process.husing namespace std;int main(void){Process tpm;const std::string name = D:/Test/Image.exe;const std::string programme = Image.exe;if (!tpm.startProcess(name)) {cout << Start Processe Error! << endl;}Sleep(5000);if(!tpm.terminateProcess(programme)){cout << Terminate Process Error! << endl;}return 0;}
Output: