Programming/WinAPI2011. 9. 23. 01:23

Timer with Callback procedure using CreateTimerQueueTimer()

 

The prototype of CreateTimerQueueTimer supported by after windows 2000

BOOL WINAPI CreateTimerQueueTimer(
  __out     PHANDLE phNewTimer,
  __in_opt  HANDLE TimerQueue,
  __in      WAITORTIMERCALLBACK Callback,
  __in_opt  PVOID Parameter,
  __in      DWORD DueTime,
  __in      DWORD Period,
  __in      ULONG Flags
);

 

  1. phNewTimer : the handler that you want to use, ex) GetCurrentThread() means put the new-timer into the current thread.
  2. HANDLE TimerQueue : Optional, usually NULL , if it's null use a default Queue. 
  3. WAITORTIMERCALLBACK Callback : put the name of Callback function, the prototype of the Callback function is

    VOID CALLBACK WaitOrTimerCallback( __in  PVOID lpParameter, __in  BOOLEAN TimerOrWaitFired );

  4. Parameter : the parameter going to CALLBACK function int "lpParameter" (See above)
  5. Duetime : the amount of time in milliseconds relative to the current time, usually it's "0"
  6. Period : the period of the timer, in millisecond.(Elapse time)
  7. Flags : WT_EXECUTEDEFAULT, WT_EXECUTEINPERSISTENTTHREAD(very useful)

 

Example by Sebeom Park

 

struct ownDateSystem // Sebeom defined this.
{
 int date;
} oDate;

char str[9];	      // to save current SystemTime
SYSTEMTIME st;        // to get CurrentLocalTime of the system.

void CALLBACK TimerProc(PVOID lpParametar,BOOLEAN TimerOrWaitFired) // it works perfectly well
{
 int currentTime = GetCurrentLocalTime(st);		
 struct ownDateSystem *reservedStructure = (ownDateSystem *)lpParametar;	// casting from PVOID to "ownDateSystem"
 int reservedTime = reservedStructure->date;
 if(currentTime == reservedTime)
 {
 RunProcess("C:\\HNC\\Hwp70\\Hwp.exe",NULL);
 }
 else
 return;
 
 // MessageBox(NULL,"Hello","Hello",MB_OK);
} 


unsigned int GetCurrentLocalTime(SYSTEMTIME &st)		// get CurrentTime
{
 unsigned int currentTime;
 GetLocalTime(&st);
 wsprintf(str,"%d%d%d%d",st.wMonth,st.wDay,st.wHour,st.wMinute); // get the formet Month/Day/Hour/Min
 currentTime = atoi(str); // change the string to int
 return currentTime;
}



BOOL AddTimer(unsigned int setDate, HWND hWnd) // for testing.. practice
{
 oDate.date = 923038;
 HANDLE currentHandler = GetCurrentThread();

	
 CreateTimerQueueTimer(&currentHandler,NULL,TimerProc,&oDate,0,300,WT_EXECUTEDEFAULT); // Get Current Thread


 //wsprintf(str,"%d %d %d %d",st.wMonth,st.wDay,st.wHour,st.wMinute);
 return TRUE; 
}

'Programming > WinAPI' 카테고리의 다른 글

Practice  (0) 2011.09.04
Posted by 박세범