Skip to content

Commit

Permalink
#811: dinamically load GetTickCount64 at runtime in order to produce …
Browse files Browse the repository at this point in the history
…exes/wheels compatible with Win XP
  • Loading branch information
giampaolo committed Jul 6, 2016
1 parent b6b8c3e commit e481df9
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ psutil_get_nic_addresses() {
*/


static ULONGLONG (*psutil_GetTickCount64)(void) = NULL;

/*
* Return a Python float representing the system uptime expressed in seconds
* since the epoch.
Expand All @@ -203,6 +205,8 @@ psutil_boot_time(PyObject *self, PyObject *args) {
time_t pt;
FILETIME fileTime;
long long ll;
HINSTANCE hKernel32;
psutil_GetTickCount64 = NULL;

GetSystemTimeAsFileTime(&fileTime);

Expand All @@ -223,13 +227,23 @@ psutil_boot_time(PyObject *self, PyObject *args) {
+ fileTime.dwLowDateTime;
pt = (time_t)((ll - 116444736000000000ull) / 10000000ull);

#if (_WIN32_WINNT >= 0x0600) // Windows Vista
uptime = GetTickCount64() / (ULONGLONG)1000.00f;
#else
// GetTickCount() time will wrap around to zero if the
// system is run continuously for 49.7 days.
uptime = GetTickCount() / 1000.00f;
#endif
// GetTickCount64() is Windows Vista+ only. Dinamically load
// GetTickCount64() at runtime. We may have used
// "#if (_WIN32_WINNT >= 0x0600)" pre-processor but that way
// the produced exe/wheels cannot be used on Windows XP, see:
// https://github.com/giampaolo/psutil/issues/811#issuecomment-230639178
hKernel32 = GetModuleHandleW(L"KERNEL32");
psutil_GetTickCount64 = (void*)GetProcAddress(hKernel32, "GetTickCount64");
if (psutil_GetTickCount64 != NULL) {
// Windows >= Vista
uptime = psutil_GetTickCount64() / (ULONGLONG)1000.00f;
}
else {
// Windows XP.
// GetTickCount() time will wrap around to zero if the
// system is run continuously for 49.7 days.
uptime = GetTickCount() / 1000.00f;
}

return Py_BuildValue("d", (double)pt - (double)uptime);
}
Expand Down

0 comments on commit e481df9

Please sign in to comment.