// RTTimerP.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include "Tlhelp32.h" #include "pkfuncs.h" #include "RTTimerDll.h" UINT timerID; UINT timerInterval= 10; //default 10ms DWORD dwTotalTick=0; LARGE_INTEGER nFreq; LARGE_INTEGER TimerTick; DWORD TimerISTNum=0; /* Sharememory_UINT32 Address: 0: The average of time span of twice calllback function called by periodically upon expiration of real-time timer1 (unit:milliseconds) 1: The minimum of time span of twice calllback function called by periodically upon expiration of real-time timer1 (unit:milliseconds) 2: The maximum of time span of twice calllback function called by periodically upon expiration of real-time timer1 (unit:milliseconds) 10: Records the starting time difference of 2 program (RTTPrj.exe and RTTimerP.exe) */ void GetMinTick(DWORD dwtick) { DWORD dwvalue; RTT_ReadDWORD(1,&dwvalue); if(dwtickdwvalue) RTT_WriteDWORD(2,dwtick); } void GetAvgTick(DWORD dwtick) { DWORD dwvalue; if(TimerISTNum>=8000) { dwTotalTick=0; RTT_WriteDWORD(0,0); TimerISTNum=1; } else TimerISTNum++; dwTotalTick+=dwtick; dwvalue=(DWORD)((double)(dwTotalTick/TimerISTNum)); RTT_WriteDWORD(0,dwvalue); } void CALLBACK CallProc(UINT uID, UINT uMsg,DWORD dwUser,DWORD dw1,DWORD dw2) { LARGE_INTEGER nEndTime; DWORD dwvalue; QueryPerformanceCounter(&nEndTime); double ddiff=(double)(nEndTime.QuadPart-TimerTick.QuadPart)*1000*1000/(double)nFreq.QuadPart; if(dwTotalTick) { GetMinTick((DWORD)ddiff); GetMaxTick((DWORD)ddiff); } GetAvgTick((DWORD)ddiff); QueryPerformanceCounter(&TimerTick); } int _tmain(int argc, _TCHAR* argv[]) { HANDLE hMutex = CreateMutex(NULL, false, L"USERTTIMER"); if (GetLastError() == ERROR_ALREADY_EXISTS) { CloseHandle(hMutex); return -1; } RTT_WriteDWORD(10,GetTickCount()); //Save RTTimerP start time tick value to the share memory slot 10 of DWORD data if(argc>1) { timerInterval=_wtoi(argv[1]); QueryPerformanceFrequency(&nFreq); /*Initialize the global variables*/ RTT_WriteDWORD(0,0); RTT_WriteDWORD(1,0xFFFFFFFF); RTT_WriteDWORD(2,0); HANDLE hConnect = CreateEvent(NULL, TRUE, FALSE, L"CLOSERTTIMER"); timeBeginPeriod(1); //call timeSetEvent and input user setting value for create time event timerID = timeSetEvent(timerInterval,1, CallProc, 0 ,TIME_PERIODIC); QueryPerformanceCounter(&TimerTick); WaitForSingleObject(hConnect, INFINITE); Sleep(1); timeKillEvent(timerID); timeEndPeriod(1); } else printf("Invalid parameter\r\n"); return 0; }