试下写个 demo 程序来监控。断电情况(程序挂掉),发送告警到微信、钉钉等;屏幕关闭情况,根据
"GetSystemMetrics(SM_CMONITORS)" 获取屏幕数量。
现在gpt那么火,润色下:你作为一个资深windows开发,试下写个 exe程序来监控以下两个情况。1. 断电情况(程序挂掉),发送告警到微信、钉钉等;2.
多个屏幕时,有一个屏幕关闭情况,根据 GetSystemMetrics(SM_CMONITORS) 获取屏幕数量,发送告警。大概程序就有了:
ps:但是断电情况,还是你自己在其他机器认真写个脚本,老老实实地 ping 去吧~
#include
#include
#include
#include
#include
#include
#pragma comment (lib, "urlmon.lib")
bool IsProgramRunning(const TCHAR* szExeName)
{
// 创建快照
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
{
return false;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
// 检查每个进程
if (!Process32First(hSnap, &pe32))
{
CloseHandle(hSnap);
return false;
}
do
{
if (_tcsicmp(pe32.szExeFile, szExeName) == 0)
{
CloseHandle(hSnap);
return true;
}
} while (Process32Next(hSnap, &pe32));
CloseHandle(hSnap);
return false;
}
void SendAlert(const std::string& message)
{
// TODO: implement code to send alert message to WeChat or DingTalk
}
void CheckProgramAndMonitors()
{
// 监控目标程序的状态
while (true)
{
if (!IsProgramRunning(_T("your_program.exe")))
{
// 目标程序已退出,发送报警信息
SendAlert("Your program has terminated unexpectedly");
}
// 检查屏幕数量
int monitorCount = GetSystemMetrics(SM_CMONITORS);
// 如果屏幕数量小于预期值,发送报警
if (monitorCount < 2)
{
SendAlert("One of the monitors has been disconnected");
}
Sleep(5000); // 每隔5秒检查一次
}
}
int main()
{
CheckProgramAndMonitors();
return 0;
}