//===================================================== file = wakeup.c =====
//=  Program to start a time that will wake-up a Windows PC                 =
//===========================================================================
//=  Notes:                                                                 =
//=    1) From http://msdn.microsoft.com/en-us/library/ms687008(VS.85).aspx =
//=       See also http://msdn.microsoft.com/en-us/library/aa373235.aspx    =
//=    2) Monitor will not power-up and that system will automatically go   =
//=       back to sleep after 2 minutes (if it was sleeping to begin with)  =
//=-------------------------------------------------------------------------=
//   Build: cl wakeup.c (will not build with bcc32)                         =
//=-------------------------------------------------------------------------=
//=  Execute: wakeup (and then put PC to sleep)                             =
//=-------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (08/02/08) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>        // Needed for printf() and feof()
#include <windows.h>      // Needed for Windows stuff

//===========================================================================
//=  Main program                                                           =
//===========================================================================
int main()
{
  HANDLE hTimer = NULL;         // Timer handle
  LARGE_INTEGER liDueTime;      // Timer variable

  // Create a waitable timer.
  hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
  if (NULL == hTimer)
  {
      printf("CreateWaitableTimer failed (%d) \n", GetLastError());
      return -1;
  }

  // Set a timer to wait for 60 seconds.
  liDueTime.QuadPart=-600000000LL;
  printf("Waiting for 60 seconds and then trigger a wake-up...\n");
  if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, TRUE))
  {
      printf("SetWaitableTimer failed (%d) \n", GetLastError());
      return -1;
  }

  // Wait for the timer -- wake-up (if sleeping) will occur when timer pops
  //  - Note that monitor will not power-up and that system will automatically
  //    go back to sleep after 2 minutes (if it was sleeping to begin with)
  if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
    printf("WaitForSingleObject failed (%d) \n", GetLastError());
  else
    printf("Timer was signaled \n");

  // Return
  return(0);
}

