summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-02-21 10:41:17 -0800
committerGiampaolo Rodola <g.rodola@gmail.com>2019-02-21 10:41:17 -0800
commitb29f0620f14ecfc1419387de04e283015d96ae31 (patch)
treebf16320143936a0e5639d8834fd203fd47f8f243
parent0a30dc9dc2693ffe9aa54126eb684711d950c3f1 (diff)
downloadpsutil-b29f0620f14ecfc1419387de04e283015d96ae31.tar.gz
set Windows version constants globally; get rid of deprecated GetVersionEx
-rw-r--r--psutil/_psutil_windows.c2
-rw-r--r--psutil/arch/windows/global.c60
-rw-r--r--psutil/arch/windows/global.h12
-rw-r--r--psutil/arch/windows/ntextapi.h23
-rw-r--r--psutil/arch/windows/process_handles.c9
5 files changed, 79 insertions, 27 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 3908884e..c0d73645 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -3758,7 +3758,7 @@ void init_psutil_windows(void)
// set SeDebug for the current process
psutil_set_se_debug();
psutil_setup();
- if (psutil_loadlibs() != 0)
+ if (psutil_load_globals() != 0)
return NULL;
#if PY_MAJOR_VERSION >= 3
diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c
index bb9970fe..da3bdc6e 100644
--- a/psutil/arch/windows/global.c
+++ b/psutil/arch/windows/global.c
@@ -2,6 +2,10 @@
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
+ *
+ * This code is executed on import. It loads private/undocumented
+ * Windows APIs and sets Windows version constants so that they are
+ * available globally.
*/
#include <windows.h>
@@ -10,6 +14,9 @@
#include "global.h"
+// Needed to make this globally visible.
+int PSUTIL_WINVER;
+
// A wrapper around GetModuleHandle and GetProcAddress.
PVOID
psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) {
@@ -52,15 +59,11 @@ psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) {
}
-/*
- * This is executed on import and loads Windows APIs so that they
- * are available globally.
- */
+static int
psutil_loadlibs() {
/*
* Mandatory.
*/
-
psutil_NtQuerySystemInformation = psutil_GetProcAddressFromLib(
"ntdll.dll", "NtQuerySystemInformation");
if (psutil_NtQuerySystemInformation == NULL)
@@ -108,10 +111,14 @@ psutil_loadlibs() {
if (! psutil_GetExtendedUdpTable)
return 1;
+ psutil_RtlGetVersion = psutil_GetProcAddressFromLib(
+ "ntdll.dll", "RtlGetVersion");
+ if (! psutil_RtlGetVersion)
+ return 1;
+
/*
* Optional.
*/
-
// minimum requirement: Win Vista
psutil_GetTickCount64 = psutil_GetProcAddress(
"kernel32", "GetTickCount64");
@@ -127,3 +134,44 @@ psutil_loadlibs() {
PyErr_Clear();
return 0;
}
+
+
+static int
+psutil_set_winver() {
+ RTL_OSVERSIONINFOEXW versionInfo;
+ ULONG maj;
+ ULONG min;
+
+ versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
+ memset(&versionInfo, 0, sizeof(RTL_OSVERSIONINFOEXW));
+ psutil_RtlGetVersion((PRTL_OSVERSIONINFOW)&versionInfo);
+ maj = versionInfo.dwMajorVersion;
+ min = versionInfo.dwMinorVersion;
+ if (maj == 5 && min == 1)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_XP;
+ else if (maj == 5 && min == 2)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_SERVER_2003;
+ else if (maj == 6 && min == 0)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_VISTA; // or Server 2008
+ else if (maj == 6 && min == 1)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_7;
+ else if (maj == 6 && min == 2)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_8;
+ else if (maj == 6 && min == 3)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_8_1;
+ else if (maj == 10 && min == 0)
+ PSUTIL_WINVER = PSUTIL_WINDOWS_10;
+ else
+ PSUTIL_WINVER = PSUTIL_WINDOWS_NEW;
+ return 0;
+}
+
+
+int
+psutil_load_globals() {
+ if (psutil_loadlibs() != 0)
+ return 1;
+ if (psutil_set_winver() != 0)
+ return 1;
+ return 0;
+}
diff --git a/psutil/arch/windows/global.h b/psutil/arch/windows/global.h
index 8d828776..378026db 100644
--- a/psutil/arch/windows/global.h
+++ b/psutil/arch/windows/global.h
@@ -6,6 +6,16 @@
#include <windows.h>
-int psutil_loadlibs();
+extern int PSUTIL_WINVER;
+#define PSUTIL_WINDOWS_XP 51
+#define PSUTIL_WINDOWS_SERVER_2003 52
+#define PSUTIL_WINDOWS_VISTA 60
+#define PSUTIL_WINDOWS_7 61
+#define PSUTIL_WINDOWS_8 62
+#define PSUTIL_WINDOWS_8_1 63
+#define PSUTIL_WINDOWS_10 100
+#define PSUTIL_WINDOWS_NEW MAXLONG
+
+int psutil_load_globals();
PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname);
PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname);
diff --git a/psutil/arch/windows/ntextapi.h b/psutil/arch/windows/ntextapi.h
index e0006c7b..84c5a2e1 100644
--- a/psutil/arch/windows/ntextapi.h
+++ b/psutil/arch/windows/ntextapi.h
@@ -400,22 +400,19 @@ typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)(
DWORD ProcessInformationClass,
PVOID ProcessInformation,
DWORD ProcessInformationLength,
- PDWORD ReturnLength
-);
+ PDWORD ReturnLength);
typedef NTSTATUS (NTAPI *_NtQuerySystemInformation)(
ULONG SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
- PULONG ReturnLength
-);
+ PULONG ReturnLength);
typedef NTSTATUS (NTAPI *_NtSetInformationProcess)(
HANDLE ProcessHandle,
DWORD ProcessInformationClass,
PVOID ProcessInformation,
- DWORD ProcessInformationLength
-);
+ DWORD ProcessInformationLength);
typedef PSTR (NTAPI * _RtlIpv4AddressToStringA)(
struct in_addr *Addr,
@@ -431,8 +428,7 @@ typedef DWORD (WINAPI * _GetExtendedTcpTable)(
BOOL bOrder,
ULONG ulAf,
TCP_TABLE_CLASS TableClass,
- ULONG Reserved
-);
+ ULONG Reserved);
typedef DWORD (WINAPI * _GetExtendedUdpTable)(
PVOID pUdpTable,
@@ -440,8 +436,7 @@ typedef DWORD (WINAPI * _GetExtendedUdpTable)(
BOOL bOrder,
ULONG ulAf,
UDP_TABLE_CLASS TableClass,
- ULONG Reserved
-);
+ ULONG Reserved);
typedef DWORD (CALLBACK *_GetActiveProcessorCount)(
WORD GroupNumber);
@@ -454,7 +449,10 @@ typedef NTSTATUS (NTAPI *_NtQueryObject)(
OBJECT_INFORMATION_CLASS ObjectInformationClass,
PVOID ObjectInformation,
ULONG ObjectInformationLength,
- PULONG ReturnLength
+ PULONG ReturnLength);
+
+typedef NTSTATUS (WINAPI *_RtlGetVersion) (
+ PRTL_OSVERSIONINFOW lpVersionInformation
);
/*
@@ -499,4 +497,7 @@ _NtQueryObject \
_GetLogicalProcessorInformationEx \
psutil_GetLogicalProcessorInformationEx;
+_RtlGetVersion \
+ psutil_RtlGetVersion;
+
#endif // __NTEXTAPI_H__
diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c
index f295f18d..6f133ef9 100644
--- a/psutil/arch/windows/process_handles.c
+++ b/psutil/arch/windows/process_handles.c
@@ -502,16 +502,9 @@ cleanup:
*/
PyObject *
psutil_get_open_files(long dwPid, HANDLE hProcess) {
- OSVERSIONINFO osvi;
-
- ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- GetVersionEx(&osvi);
-
// Threaded version only works for Vista+
- if (osvi.dwMajorVersion >= 6)
+ if (PSUTIL_WINVER >= PSUTIL_WINDOWS_VISTA)
return psutil_get_open_files_ntqueryobject(dwPid, hProcess);
else
return psutil_get_open_files_getmappedfilename(dwPid, hProcess);
}
-