窗口看门狗与独立看门狗的代码
时间: 2024-01-05 22:03:44 浏览: 128
窗口看门狗代码:
```
#include <Windows.h>
int main()
{
HWND hwnd = FindWindow(NULL, "Window Title"); // 替换为目标窗口的标题
if (hwnd == NULL)
{
MessageBox(NULL, "Window not found!", "Error", MB_OK);
return 1;
}
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (hProcess == NULL)
{
MessageBox(NULL, "Failed to open process!", "Error", MB_OK);
return 1;
}
while (true)
{
if (IsWindowVisible(hwnd))
{
Sleep(1000); // 每隔一秒检查一次窗口是否存在
}
else
{
TerminateProcess(hProcess, 0); // 关闭目标进程
CloseHandle(hProcess);
return 0;
}
}
}
```
独立看门狗代码:
```
#include <Windows.h>
int main()
{
DWORD pid = GetCurrentProcessId();
while (true)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL)
{
MessageBox(NULL, "Process not found!", "Error", MB_OK);
return 1;
}
DWORD exitCode;
if (GetExitCodeProcess(hProcess, &exitCode) && exitCode != STILL_ACTIVE)
{
// 进程已退出
CloseHandle(hProcess);
return 0;
}
CloseHandle(hProcess);
Sleep(1000); // 每隔一秒检查一次进程是否存在
}
}
```
阅读全文