Windows programming: Turn on/off/Traverse Program Class Code (c + +)

Source: Internet
Author: User
Tags bool printf

class contains 4 functions, starts the program, iterates through all the processes, closes the program, and traverses the dynamic link library that the process relies on.

Example: Image.exe is a pre-generated executable program (EXE) that starts the program, 5 seconds apart, and closes the program.

Use the method to participate in the test program.

Code:

* * * process.h * * Created on:2014.06.08 * author:spike//*vs 2012*/#ifndef TRAVERSEPR Ocessmodel_h #define TRAVERSEPROCESSMODEL_H #include <iostream> #include <iomanip> #include  
      
;string> #include <map> #include <windows.h> #include <TlHelp32.h>//snapshot  
    Class Process {Public:bool startprocess (const std::string _name);  
    BOOL TerminateProcess (const std::string _name);  
    BOOL Traversemodels (const std::string _name);  
BOOL Traverseprocesses (std::map<std::string, int>& _nameid);  
      
};      #endif//traverseprocessmodel_h/* process.cpp * * Created on:2014.06.08 *  
      
Author:spike * */*vs 2012*/#include "process.h" using namespace std; BOOL Process::startprocess (const std::string name) {startupinfo si;//Parameter Settings memset (&si, 0, sizeof (STAR TUpinfo));  
    SI.CB = sizeof (STARTUPINFO);  
    Si.dwflags = Startf_useshowwindow;  
      
    Si.wshowwindow = Sw_show; Process_information Pi; 
    The argument ends/*printf ("Please enter the name of process to start:"); 
    std::string name; CIN >> name;*/if (! CreateProcessA (null, (LPSTR) name.c_str (), NULL, NULL, FALSE, 0,NULL,NULL,&AMP;SI,&AMP;PI)) {cout<< "Start Process error! "  
        <<endl;  
    return false; else {cout<< "Start Process sucess!"  
    <<endl;  
return true;   
    BOOL Process::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::e  
        NDL;;  
    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; More Highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/_nameid.insert (std::p air<string, in T> (name, id));  
    Dictionary Storage bresult = Process32Next (HPROCESSSNAP,&AMP;PE32);  
      
    } closehandle (HPROCESSSNAP);  
return true;  
      
    BOOL Process::terminateprocess (const std::string _name) {std::map<std::string, int> NameID;  
    if (!traverseprocesses (NameID)) {cout << "Traverse processes error!" << Endl;  
      
    } DWORD Dwid; 
    /*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;  
      
    BOOL Process::traversemodels (const std::string _name) {std::map<std::string, int> NameID;  
    if (!traverseprocesses (NameID)) {cout << "Traverse processes error!" << Endl;  
      
    } DWORD Dwid; 
    /*printf ("Please enter the name of a process to traverse processmodels:"); 
    std::string name;  
      
    cin >> name;*/dwid = Nameid[_name];  
    HANDLE Hmodulesnap = CreateToolhelp32Snapshot (Th32cs_snapmodule, Dwid); If(Hmodulesnap = = INVALID_HANDLE_VALUE) {printf ("createtoolhelp32snapshoterror!  
        \ n ");  
    return false;  
    } MODULEENTRY32 module32;  
    module32.dwsize = sizeof (MODULE32);  
      
    BOOL bresult = Module32first (Hmodulesnap, &module32); while (bresult) {std::wcout << "Module:" << std::left << std::setw () << Module32.szmodu  
        Le << "" << 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.h"  
      
using 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:

Author: csdn Blog spike_king

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.