#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <pdh.h>
char* GetUptime(int iTickCount);
#define SAMPLE_INTERVAL_MS 1000
#define MAXPATH 80
int __cdecl _tmain (int argc, TCHAR **argv)
{
HQUERY hQuery;
HCOUNTER *pCounterHandle;
PDH_STATUS pdhStatus;
PDH_FMT_COUNTERVALUE fmtValue;
DWORD ctrType;
SYSTEMTIME stSampleTime;
PDH_BROWSE_DLG_CONFIG BrowseDlgData;
CHAR szPathBuffer[MAXPATH];
int nRetCode = 0;
// Open the query object.
pdhStatus = PdhOpenQuery (0, 0, &hQuery);
// Allocate the counter handle array. Allocate room for
// one handle per command line arg, not including the
// executable file name.
pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));
strcpy(szPathBuffer,"\\System\\System Up Time");
pdhStatus = PdhAddCounter (hQuery,
szPathBuffer,
0,
pCounterHandle);
// "Prime" counters that need two values to display a
// formatted value.
pdhStatus = PdhCollectQueryData (hQuery);
// Print counter values until a key is pressed.
while (!_kbhit()) {
// Wait one interval.
Sleep(SAMPLE_INTERVAL_MS);
// Get the sample time.
GetLocalTime (&stSampleTime);
// Get the current data values.
pdhStatus = PdhCollectQueryData (hQuery);
// Print the time stamp for the sample.
_tprintf (TEXT("\n %2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d.%3.3d "),
stSampleTime.wMonth,
stSampleTime.wDay,
stSampleTime.wYear,
stSampleTime.wHour,
stSampleTime.wMinute,
stSampleTime.wSecond,
stSampleTime.wMilliseconds);
// Get the current value of this counter.
pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle,
PDH_FMT_DOUBLE,
&ctrType,
&fmtValue);
if (pdhStatus == ERROR_SUCCESS) {
//_tprintf (TEXT(",\"%.20g\""), fmtValue.doubleValue);
_tprintf(TEXT(", %s"),GetUptime((int)fmtValue.doubleValue));
} else {
// Print the error value.
_tprintf (TEXT(".\"-1\""));
}
}
// Close the query.
pdhStatus = PdhCloseQuery (hQuery);
return nRetCode;
}
char* GetUptime(int tickCount)
{
int nTime[5];
char szUptime[1000];
nTime[0] = tickCount % 60; // Seconds
nTime[1] = tickCount / 60 % 60; // Minutes
nTime[2] = tickCount / 60 / 60 % 24; // Hours
nTime[3] = tickCount / 60 / 60 / 24 % 7; // Days
nTime[4] = tickCount / 60 / 60 / 24 / 7 % 52; // Weeks
memset(szUptime, 0, sizeof(szUptime));
for(int i = 4; i >= 0; i--)
{
char* szLabel;
char tmpUptime[4];
if (i == 4) (nTime[4] > 1 || nTime[4] == 0) ? szLabel = " Weeks, " : szLabel = " Week, ";
else if (i == 3) (nTime[3] > 1 || nTime[3] == 0) ? szLabel = " Days, " : szLabel = " Day, ";
else if (i == 2) (nTime[2] > 1 || nTime[2] == 0) ? szLabel = " Hours, " : szLabel = " Hour, ";
else if (i == 1) (nTime[1] > 1 || nTime[1] == 0) ? szLabel = " Minutes, " : szLabel = " Minute, ";
else if (i == 0) (nTime[0] > 1 || nTime[0] == 0) ? szLabel = " Seconds" : szLabel = " Second";
memset(tmpUptime, 0, sizeof(tmpUptime));
itoa(nTime[i], tmpUptime, 10);
strcat(strcat(szUptime, tmpUptime), szLabel);
}
return szUptime;
}
Sprawdz to ;)
nie dam glowy ze sie nie przekreci licznik po tych 49.7 dniach :)
ale kod jest z msdn.