Windows编程:开启/封锁/遍历措施的类 代码(C++)
当前位置:以往代写 > C/C++ 教程 >Windows编程:开启/封锁/遍历措施的类 代码(C++)
2019-06-13

Windows编程:开启/封锁/遍历措施的类 代码(C++)

Windows编程:开启/封锁/遍历措施的类 代码(C++)

类包括4个函数, 启动措施, 遍历所有历程, 封锁措施, 遍历历程依赖的动态链接库.

示例: Image.exe是预先生成的可执行措施(exe), 启动措施, 隔断5秒, 封锁措施.

利用要领介入测试措施.

代码:

/* 
 * process.h 
 * 
 *  Created on: 2014.06.08 
 *      Author: Spike 
 */
      
/*vs 2012*/
      
#ifndef TRAVERSEPROCESSMODEL_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; //参数配置  
    memset(&si, 0, sizeof(STARTUPINFO));  
    si.cb = sizeof(STARTUPINFO);  
    si.dwFlags = STARTF_USESHOWWINDOW;  
    si.wShowWindow = SW_SHOW;  
      
    PROCESS_INFORMATION pi; //参数竣事  
      
    /*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,&si,&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::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;  
    //
/* 
 * 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;  
}

输出:

Windows编程:开启/封闭/遍历法子的类 代码(C++)

作者:csdn博客 Spike_King

    关键字:

在线提交作业