C++ 读取配置文件结束指定进程
#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++ 读取配置文件结束指定进程的更多相关文章
- windows xp/7命令提示符强制结束指定进程
开始----“运行 ”输入cmd ,然后在命令提示符下输入tasklist,出现如下列表: Image Name PID Session Name ...
- Delphi 如何让程序获取权限结束指定进程?
比如说让程序结束进程中360sd.exe 获取权限,否则会拒绝访问, 要怎么写? 补充: 这段代码中……点击按钮后结束不了360进程! unit Unit1;interfaceusesWindow ...
- Python测试进阶——(5)Python程序监控指定进程的CPU和内存利用率
用Python写了个简单的监控进程的脚本monitor190620.py,记录进程的CPU利用率和内存利用率到文件pid.csv中,分析进程运行数据用图表展示. 脚本的工作原理是这样的:脚本读取配置文 ...
- Core 读取配置文件
新建控制台 static void Main(string[] args) { Console.WriteLine("Hello World!"); //获取应用程序的当前工作目录 ...
- Java读取配置文件的方式
Java读取配置文件的方式-笔记 1 取当前启动文件夹下的配置文件 一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
- java 4种方式读取配置文件 + 修改配置文件
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...
- nova读取配置文件流程
在我们安装nova的过程中,设置它的配置文件/etc/nova/nova.conf是必不可少的一步.配置好nova.conf文件,nova-compute.nova-network等服务才 ...
随机推荐
- mysqli存储过程
<?php$link = mysqli_connect('localhost','root','','chinatupai'); $sql = "call getEmail('000 ...
- LeetCode Day 6
LeetCode0006 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ,指定行数为 3 时,排 ...
- java 面向对象 购物车
一个商城包括多个商品.多个用户.拥有销售商品.展示商品和查找商品功能.2) 一个用户拥有一个购物车,购物车具有结算功能.3) 商城具有名称,静态字符串类型4) 用户类是抽象类,两个子 ...
- crack|erosion|strip|
V-ERG (使)破裂;(使)裂开;(使)断裂 If something hard cracks, or if you crack it, it becomes slightly damaged, w ...
- UnitTest测试框架-操作步骤
一.UnitTest 1. TestCase 说明:测试用例 1.新建类并集成unittest.TestCase 2. TestSuite 说明:测试套件(多条用例) 方法: 1. 实例化 suite ...
- <USACO06NOV>玉米田Corn Fields
状压emm 二进制真有趣 来自dp垃圾的欣喜 Description 农民 John 购买了一处肥沃的矩形牧场,分成M*N(1 <= M <= 12; 1 <= N <= 12 ...
- 理解 Redux 中间件机制
Redux 的 action 是一个 JS 对象,它表明了如何对 store 进行修改.但是 Redux 的中间件机制使action creator 不光可以返回 action 对象,也可以返回 ac ...
- 吴裕雄--天生自然 PYTHON数据分析:钦奈水资源管理分析
df = pd.read_csv("F:\\kaggleDataSet\\chennai-water\\chennai_reservoir_levels.csv") df[&quo ...
- js中判断为false的情况
document.write((new Boolean())+"<br />"); document.write((new Boolean(" ...
- Git学习小结 ~ Lethe's Blog
学习自https://www.liaoxuefeng.com/wiki/896043488029600 一.创建版本库 (1) git init 初始化一个Git仓库 (2)添加文件到Git仓库,分两 ...