c++实现Windows后台服务,从.txt文件读取进程名和绝对路径,判断进程是否在运行,定时监控和重启
时间: 2023-06-19 10:09:17 浏览: 166
C++后台程序实时性能监控
要实现Windows后台服务,可以使用Windows服务程序来实现。下面是一个简单的实现步骤:
1. 创建一个Windows服务程序的工程,并添加头文件和库文件。
2. 在服务程序的入口函数中,调用StartServiceCtrlDispatcher函数来注册服务控制处理函数。
3. 实现服务控制处理函数,在处理函数中实现服务的启动、停止、暂停、继续等操作。
4. 在服务启动时,读取.txt文件中的进程名和绝对路径,并将其保存到一个数据结构中,例如一个vector。
5. 使用CreateToolhelp32Snapshot函数获取系统中正在运行的进程列表,并与保存在数据结构中的进程名进行比较,判断进程是否在运行。
6. 如果进程不在运行,则使用CreateProcess函数启动进程。
7. 使用定时器功能,定时监控进程是否在运行,如果不在运行,则重启进程。
8. 在服务停止时,释放资源并结束服务。
下面是一个简单的代码示例,仅供参考:
```cpp
#include <windows.h>
#include <tchar.h>
#include <vector>
#include <string>
#include <fstream>
#include <tlhelp32.h>
#define SERVICE_NAME _T("ProcessMon")
SERVICE_STATUS g_ServiceStatus = {0};
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
std::vector<std::pair<std::string, std::string>> g_ProcessList;
VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
VOID WINAPI ServiceCtrlHandler(DWORD);
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
BOOL GetProcessPath(DWORD dwProcessId, std::string& path);
int _tmain(int argc, TCHAR* argv[])
{
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
{NULL, NULL}
};
if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
{
return GetLastError();
}
return 0;
}
VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
DWORD Status = E_FAIL;
g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
if (g_StatusHandle == NULL)
{
goto EXIT;
}
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwServiceSpecificExitCode = 0;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_ServiceStopEvent == NULL)
{
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
goto EXIT;
}
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(g_ServiceStopEvent);
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
EXIT:
return;
}
VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
{
switch (CtrlCode)
{
case SERVICE_CONTROL_STOP:
if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
break;
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
SetEvent(g_ServiceStopEvent);
break;
case SERVICE_CONTROL_PAUSE:
g_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
break;
case SERVICE_CONTROL_CONTINUE:
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
break;
case SERVICE_CONTROL_SHUTDOWN:
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
SetEvent(g_ServiceStopEvent);
break;
default:
break;
}
}
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
{
std::ifstream file("process_list.txt");
std::string line;
while (std::getline(file, line))
{
size_t pos = line.find(",");
if (pos != std::string::npos)
{
std::string processName = line.substr(0, pos);
std::string processPath = line.substr(pos + 1);
g_ProcessList.push_back(std::make_pair(processName, processPath));
}
}
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
{
for (auto& process : g_ProcessList)
{
DWORD dwProcessId = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32))
{
do
{
if (_stricmp(pe32.szExeFile, process.first.c_str()) == 0)
{
dwProcessId = pe32.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
if (dwProcessId == 0)
{
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
si.cb = sizeof(STARTUPINFO);
TCHAR szCmdline[MAX_PATH];
snprintf(szCmdline, MAX_PATH, "%s", process.second.c_str());
if (CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
}
Sleep(5000);
}
return ERROR_SUCCESS;
}
BOOL GetProcessPath(DWORD dwProcessId, std::string& path)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
if (hProcess == NULL)
{
return FALSE;
}
TCHAR szPath[MAX_PATH];
DWORD dwSize = MAX_PATH;
if (QueryFullProcessImageName(hProcess, 0, szPath, &dwSize))
{
path = szPath;
return TRUE;
}
CloseHandle(hProcess);
return FALSE;
}
```
阅读全文