#include <windows.h>
#include <pkfuncs.h>
#define WATCHDOG_NAME L"wd_critproc"
#define WATCHDOG_PERIOD 5000 // milliseconds
#define WATCHDOG_WAIT_TIME 2000 // milliseconds
//WDOG_NO_DFLT_ACTION, WDOG_KILL_PROCESS, WDOG_RESET_DEVICE
#define WATCHDOG_DEFAULT_ACTION WDOG_RESET_DEVICE
#define MAX_COUNT 10
int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
HANDLE hWatchDogTimer=NULL;
LPCWSTR pszWatchDogName=WATCHDOG_NAME;
DWORD dwPeriod=WATCHDOG_PERIOD;
DWORD dwWait=WATCHDOG_WAIT_TIME;
DWORD dwDefaultAction=WATCHDOG_DEFAULT_ACTION;
DWORD dwCount=0;
BOOL bRet=FALSE;
wprintf((TEXT("[critproc] Critical process start
")));
wprintf((TEXT("[critproc] Calling CreateWatchDogTimer...
")));
//createwatchdogtimer api is being called here
hWatchDogTimer =
CreateWatchDogTimer(pszWatchDogName, dwPeriod,dwWait, dwDefaultAction,0,0);
if (! hWatchDogTimer)
{
wprintf((TEXT("[critproc] Invalid NULL handle, leaving app
")));
return 1;
}
//checking if the error already exists then same watchdog timer is not called
if (GetLastError()==ERROR_ALREADY_EXISTS)
{
wprintf((TEXT("[critproc] WatchDog with this name already exists,
leaving app
")));
return 1;
}
wprintf((TEXT("[critproc] Valid handle returned [0x%08x]
")),
hWatchDogTimer);
wprintf((TEXT("[critproc] Starting watchdog timer...
")));
bRet = StartWatchDogTimer(hWatchDogTimer,0);
if (! bRet)
{
wprintf((TEXT("[critproc] StartWatchDogTimer failed,
GetLastError returned 0x%x
")),GetLastError());
CloseHandle(hWatchDogTimer);
return 1;
}
wprintf((TEXT("[critproc] Watchdog timer started successfully
")));
dwCount=0;
while ((dwCount++)<MAX_COUNT)
{
BOOL bRetVal=0;
wprintf((TEXT("[critproc] Refreshing watchdog timer... [%d]
")),dwCount);
bRetVal = RefreshWatchDogTimer(hWatchDogTimer,0);
if (!bRetVal)
{
wprintf((TEXT("[critproc] Failed to refresh watchdog timer,
GetLastError returned 0x%x
")),GetLastError());
CloseHandle(hWatchDogTimer);
return 1;
}
Sleep(1000);
}
wprintf((TEXT("[critproc] Stopping watchdog timer refresh
")));
dwCount=0;
while (++dwCount)
{
wprintf((TEXT("[critproc] The watchdog should timeout in
a few seconds... [%d]
")),dwCount);
Sleep(1000);
}
wprintf((TEXT("[critproc] Leaving app (should never be here)
")));
CloseHandle(hWatchDogTimer);
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…