can anyone debug this code for me regarding a watc

2019-09-21 11:23发布

问题:

#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\r\n")));
   wprintf((TEXT("[critproc] Calling CreateWatchDogTimer...\r\n")));
//createwatchdogtimer api is being called here
 hWatchDogTimer = 
CreateWatchDogTimer(pszWatchDogName, dwPeriod,dwWait, dwDefaultAction,0,0);
 if (! hWatchDogTimer)
 {
  wprintf((TEXT("[critproc] Invalid NULL handle, leaving app\r\n")));
  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\r\n")));
   return 1;
 }
    wprintf((TEXT("[critproc] Valid handle returned [0x%08x]\r\n")),
        hWatchDogTimer);
 wprintf((TEXT("[critproc] Starting watchdog timer...\r\n")));
 bRet = StartWatchDogTimer(hWatchDogTimer,0);
 if (! bRet)
 {
        wprintf((TEXT("[critproc] StartWatchDogTimer failed,
   GetLastError returned 0x%x\r\n")),GetLastError());
  CloseHandle(hWatchDogTimer);
  return 1;
 }
 wprintf((TEXT("[critproc] Watchdog timer started successfully\r\n")));
    dwCount=0;
 while ((dwCount++)<MAX_COUNT)
 {
  BOOL bRetVal=0;
     wprintf((TEXT("[critproc] Refreshing watchdog timer... [%d]\r\n")),dwCount);
     bRetVal = RefreshWatchDogTimer(hWatchDogTimer,0);
  if (!bRetVal)
  {
   wprintf((TEXT("[critproc] Failed to refresh watchdog timer,
    GetLastError returned 0x%x\r\n")),GetLastError());
   CloseHandle(hWatchDogTimer);
   return 1;
  }

  Sleep(1000);
 }

    wprintf((TEXT("[critproc] Stopping watchdog timer refresh\r\n")));
    dwCount=0;
 while (++dwCount)
 {
        wprintf((TEXT("[critproc] The watchdog should timeout in  \
   a few seconds... [%d]\r\n")),dwCount);
  Sleep(1000);
 }
 wprintf((TEXT("[critproc] Leaving app (should never be here)\r\n")));
 CloseHandle(hWatchDogTimer);
    return 0;
}

回答1:

the error i'm getting is regarding the wmain function saying local function definitions are illegal –

You are not asking for debugging but for fixing compilation bug!

    Sleep(1000);
} 
^~~~~~~ this is what compiler is complaining about          


回答2:

Try properly defining main:

int _tmain(int argc, TCHAR* argv[])


标签: windows-ce