summaryrefslogtreecommitdiff
path: root/psutil/arch/windows/global.c
diff options
context:
space:
mode:
Diffstat (limited to 'psutil/arch/windows/global.c')
-rw-r--r--psutil/arch/windows/global.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/psutil/arch/windows/global.c b/psutil/arch/windows/global.c
index 9ef92092..4d8526e3 100644
--- a/psutil/arch/windows/global.c
+++ b/psutil/arch/windows/global.c
@@ -18,6 +18,14 @@
int PSUTIL_WINVER;
SYSTEM_INFO PSUTIL_SYSTEM_INFO;
+#define NT_FACILITY_MASK 0xfff
+#define NT_FACILITY_SHIFT 16
+#define NT_FACILITY(Status) \
+ ((((ULONG)(Status)) >> NT_FACILITY_SHIFT) & NT_FACILITY_MASK)
+#define NT_NTWIN32(status) (NT_FACILITY(Status) == FACILITY_WIN32)
+#define WIN32_FROM_NTSTATUS(Status) (((ULONG)(Status)) & 0xffff)
+
+
// A wrapper around GetModuleHandle and GetProcAddress.
PVOID
psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) {
@@ -60,6 +68,26 @@ psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) {
}
+/*
+ * Convert a NTSTATUS value to a Win32 error code and set the proper
+ * Python exception.
+ */
+PVOID
+psutil_SetFromNTStatusErr(NTSTATUS Status, const char *syscall) {
+ ULONG err;
+ char fullmsg[1024];
+
+ if (NT_NTWIN32(Status))
+ err = WIN32_FROM_NTSTATUS(Status);
+ else
+ err = psutil_RtlNtStatusToDosErrorNoTeb(Status);
+ // if (GetLastError() != 0)
+ // err = GetLastError();
+ sprintf(fullmsg, "(originated from %s)", syscall);
+ return PyErr_SetFromWindowsErrWithFilename(err, fullmsg);
+}
+
+
static int
psutil_loadlibs() {
/*
@@ -127,6 +155,11 @@ psutil_loadlibs() {
if (! psutil_NtQueryVirtualMemory)
return 1;
+ psutil_RtlNtStatusToDosErrorNoTeb = psutil_GetProcAddressFromLib(
+ "ntdll", "RtlNtStatusToDosErrorNoTeb");
+ if (! psutil_RtlNtStatusToDosErrorNoTeb)
+ return 1;
+
/*
* Optional.
*/