#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <windows.h>
#include <stdint.h>
#include <tlhelp32.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <fstream>
//#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
using namespace std;
char* time_now() //返回当前日期时间
{
time_t rawtime;
struct tm *info;
char buffer[]; time(&rawtime); info = localtime(&rawtime); strftime(buffer, , "%Y-%m-%d %H:%M:%S", info);
return buffer;
} DWORD GetProcessIdFromName(string name) //根据进程名称获取进程pid并返回进程pid
{
HANDLE hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, );
if (hsnapshot == INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp32Snapshot Error!\n");
return ;
}
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32); int flag = Process32First(hsnapshot, &pe); while (flag != )
{
if (strcmp(pe.szExeFile, name.c_str()) == )
{
return pe.th32ProcessID;
}
flag = Process32Next(hsnapshot, &pe);
}
CloseHandle(hsnapshot);
return ;
} int KillProcess(int id) //根据进程ID杀进程
{
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, id); //打开目标进程
if (hProcess == NULL) {
//wprintf(L"\nOpen Process fAiled:%d\n", GetLastError());
return -;
}
else{
DWORD ret = TerminateProcess(hProcess, );
// printf("Kill OK!\n");
if (ret == ) {
// wprintf(L"%d", GetLastError()); }
}
//结束目标进程 return -;
} void write_file(const char* timenow, int p_id, const char* p_name)// 写配置文件
{ ofstream aa;
aa.open("c:/killconfig.log");
aa << "[" << timenow << "]" << "[" << p_id << "]" << "[" << p_name << "]" <<"已被结束!"<< endl;
aa.close();
}
void ReadConfig()// 读取配置文件
{ string ppp;
ifstream ff;
ff.open("/config.ini");
while (getline(ff,ppp))
{
int a = GetProcessIdFromName(ppp.c_str());
if (a == NULL)
{
//printf("未检测到要结束的进程!\n");
continue;
}
else
{
KillProcess(a);
char* now_time = time_now();
write_file(now_time, a, ppp.c_str());//写入配置文件
printf("[%s][%d][%s]已经被结束!\n",now_time, a, ppp.c_str()); //日志打印 }
}
ff.close();
} int main()
{ while (){ ReadConfig();
Sleep(); //循环检测
} return ;
}

主要功能:  读取配置文件结束指定进程 每秒循环一次,并在C盘成成killconfig.log记录日志

C++ 读取配置文件结束指定进程的更多相关文章

  1. windows xp/7命令提示符强制结束指定进程

    开始----“运行 ”输入cmd ,然后在命令提示符下输入tasklist,出现如下列表: Image Name                     PID Session Name        ...

  2. Delphi 如何让程序获取权限结束指定进程?

    比如说让程序结束进程中360sd.exe 获取权限,否则会拒绝访问, 要怎么写?   补充: 这段代码中……点击按钮后结束不了360进程! unit Unit1;interfaceusesWindow ...

  3. Python测试进阶——(5)Python程序监控指定进程的CPU和内存利用率

    用Python写了个简单的监控进程的脚本monitor190620.py,记录进程的CPU利用率和内存利用率到文件pid.csv中,分析进程运行数据用图表展示. 脚本的工作原理是这样的:脚本读取配置文 ...

  4. Core 读取配置文件

    新建控制台 static void Main(string[] args) { Console.WriteLine("Hello World!"); //获取应用程序的当前工作目录 ...

  5. Java读取配置文件的方式

    Java读取配置文件的方式-笔记 1       取当前启动文件夹下的配置文件   一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...

  6. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  7. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

  8. java 4种方式读取配置文件 + 修改配置文件

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...

  9. nova读取配置文件流程

          在我们安装nova的过程中,设置它的配置文件/etc/nova/nova.conf是必不可少的一步.配置好nova.conf文件,nova-compute.nova-network等服务才 ...

随机推荐

  1. CF-551:部分题目总结

    题目链接:http://codeforces.com/contest/1153 A .Serval and Bus pro:给出n种公交车的首班车时间和两班车之间的时间间隔,找t时间以后的第一辆车是第 ...

  2. Java IO: FileReader和FileWriter

    作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍FileReader和FileWriter.与FileInputStream和File ...

  3. verilog的function使用

    语法: function [range] function_id;    input_declaration    other_declarations    procedural_statement ...

  4. SSM 生成mapper中xml文件:未能解析映射资源:“文件嵌套异常

    错误日记我就网上随便找个贴着: 错误一: org.springframework.beans.factory.BeanCreationException: Error creating bean wi ...

  5. string删除与查找erase,find

    s.erase( 指针位置 , 删除长度 ) 返回值修改后的string对象引用 find(string, int):第一个参数用来指示要查找的字符,第二个参数用来表示从字符串的何处开始查找子串(默认 ...

  6. SpringSecurity 如何提示错误

    1.可以通过authentication-failure-url="/login.html?error=1" 前端接收参数,根据参数提示 错误 2.前端vue this.myNam ...

  7. js实现键盘数字输入

    <html> <head> <meta charset="UTF-8"> </head> <script> functi ...

  8. Win10下JDK环境搭建的两种方法

    jdk1.8--64位官网下载的百度网盘 https://pan.baidu.com/s/1A7jYfupwMWZawb5z_RSdJg 提取码:    92eu 第一种方法(建议)    变量名:  ...

  9. JMeter之BeanShell断言---equals使用

    判断变量是否为root if(!"${User}".equals("root")){ Failure=true; FailureMessage="ER ...

  10. MyBatis之ResultMap的association和collection标签(一)

    1.先说resultMap比较容易混淆的点, 2. Map结尾是映射,Type是类型  resultType 和restltMap restulyType: 1.对应的是java对象中的属性,大小写不 ...