简单看门狗_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > 简单看门狗

简单看门狗

 2021/8/26 20:45:53  coollyj  程序员俱乐部  我要评论(0)
  • 摘要://WatchDog.cpp:此文件包含"main"函数。程序执行将在此处开始并结束。//#include<iostream>#include<comdef.h>#include<tlhelp32.h>#include<windows.h>#include<string>#include<stdio.h>#include<map>#include<thread>#include<
  • 标签:

// WatchDog.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

class="cpp">#include <iostream>

#include <comdef.h>
#include <tlhelp32.h>
#include <windows.h>
#include <string>
#include <stdio.h>
#include <map>
#include <thread>
#include <shellapi.h>

#include "IXLua.h"
#include "XLua.h"


bool TraverseProcesses(std::map<int, int>& bunchesOfProcess);
int CycleForEver();

#define  WG_CONFIG   "wg.txt"
#define  BUF_MAX   255

int main(int argc, char* argv[]) {
	while (true) {
		int ret = CycleForEver();
		printf("one cycle has fininhsed, ret=%d\n", ret);
		std::this_thread::sleep_for(std::chrono::seconds(3));
	}

	printf("quit app\n");
	return 0;
}

bool TraverseProcesses(std::map<int, int>& bunchesOfProcess) {
	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(pe32);
	HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hProcessSnap == INVALID_HANDLE_VALUE) {
		return false;
	}
	BOOL bResult = Process32First(hProcessSnap, &pe32);
	int num = 0;
	while (bResult) {
		//_bstr_t(const_cast<wchar_t*>(pe32.szExeFile));
		int id = pe32.th32ProcessID;
		bunchesOfProcess.insert(std::pair<int, int>(id, id));
		bResult = Process32Next(hProcessSnap, &pe32);
	}
	CloseHandle(hProcessSnap);
	return true;
}

int CycleForEver() {
	IXLua* lua;
	lua  = new XLua;
	bool ret = lua->init();
	ret = lua->loadFile(WG_CONFIG);
	char appname[BUF_MAX] = { 0 };
	ret = lua->getLuaVariableString("app_name", BUF_MAX, (char*)appname);

	printf("appname:%s", appname);
	//std::string m_strCmdLine = std::string(appname);

	PROCESS_INFORMATION pi;
	DWORD dwExitCode;

	if (true) {
		STARTUPINFOA si;
		memset(&si, 0, sizeof(si));
		si.cb = sizeof(si);
		si.dwFlags = STARTF_USESHOWWINDOW;
		si.wShowWindow = SW_HIDE;

		if (!CreateProcessA(
			NULL,
			(LPSTR)appname,
			NULL,
			NULL,
			FALSE,
			0,
			NULL,
			NULL,
			&si,
			&pi)) {
			printf("CreateProcess failed, cmd line=%s, last error=%d\n",
				m_strCmdLine.c_str(),
				GetLastError());
			return -1;
		}
		else {
			printf("success start cmd process, cmd=%s\n", m_strCmdLine.c_str());
		}
	}

	DWORD dwProcessId = pi.dwProcessId;
	printf("start process success, process id=%d, cmd=%s\n", (int)dwProcessId, m_strCmdLine.c_str());
	while (true) {
		std::map<int, int> processBunches;

		if (!TraverseProcesses(processBunches)) {
			printf("TraverseProcesses false\n");
		}

		bool bFind = false;

		{
			if (processBunches.find(dwProcessId) != processBunches.end()) {
				bFind = true;
			}
		}

		if (!bFind) {
			printf("can not traverse process=%d of cmd=%s, may be has quit \n", (int)dwProcessId, m_strCmdLine.c_str());
			break;
		}

		std::this_thread::sleep_for(std::chrono::seconds(3));
	}

	if (true) {
		if (!TerminateProcess(pi.hProcess, 0)) {
			printf("TerminateProcess failed, cmd=%s\n", m_strCmdLine.c_str());
		}

		if (!CloseHandle(pi.hThread)) {
			printf("CloseHandle thread failed, cmd=%s\n", m_strCmdLine.c_str());
		}

		WaitForSingleObject(pi.hProcess, INFINITE);

		if (!GetExitCodeProcess(pi.hProcess, &dwExitCode)) {
			printf("GetExitCodeProcess failed, cmd=%s\n", m_strCmdLine.c_str());
		}

		if (!CloseHandle(pi.hProcess)) {
			printf("CloseHandle hProcess failed, cmd=%s\n", m_strCmdLine.c_str());
		}
	}

	printf("process=%d of cmd=%s finished a cycle\n", (int)dwProcessId, m_strCmdLine.c_str());
	return 0;
}

?

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧:
//?? 1. 使用解决方案资源管理器窗口添加/管理文件
//?? 2. 使用团队资源管理器窗口连接到源代码管理
//?? 3. 使用输出窗口查看生成输出和其他消息
//?? 4. 使用错误列表窗口查看错误
//?? 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//?? 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

?

  • 相关文章
发表评论
用户名: 匿名