]> git.lyx.org Git - lyx.git/blobdiff - development/Win32/vld/src/utility.cpp
Add support for glue length in parskip (#12867)
[lyx.git] / development / Win32 / vld / src / utility.cpp
index e2fbddd6d7b4655eb363c170fe8d37589dc477c6..3048ba29326a95234d81f56409595b4b89e726fe 100644 (file)
@@ -1,8 +1,7 @@
 ////////////////////////////////////////////////////////////////////////////////
-//  $Id: utility.cpp,v 1.24 2006/11/18 03:12:35 dmouldin Exp $
 //
 //  Visual Leak Detector - Various Utility Functions
-//  Copyright (c) 2005-2006 Dan Moulding
+//  Copyright (c) 2005-2009 Dan Moulding
 //
 //  This library is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU Lesser General Public
 #include <cassert>
 #include <cstdio>
 #include <windows.h>
+#if _WIN32_WINNT > 0x0600 // Windows XP or earlier, no GetProcessIdOfThread()
+#include <winternl.h>
+#endif
+#ifndef __out_xcount
 #define __out_xcount(x) // Workaround for the specstrings.h bug in the Platform SDK.
+#endif
 #define DBGHELP_TRANSLATE_TCHAR
 #include <dbghelp.h>    // Provides portable executable (PE) image access functions.
 #define VLDBUILD        // Declares that we are building Visual Leak Detector.
@@ -779,3 +783,58 @@ BOOL strtobool (LPCWSTR s) {
         return FALSE;
     }
 }
+
+// _GetProcessIdOfThread - Returns the ID of the process owns the thread.
+//
+//  - thread (IN): The handle to the thread.
+//
+//  Return Value:
+//
+//    Returns the ID of the process that owns the thread. Otherwise returns 0.
+//
+DWORD _GetProcessIdOfThread (HANDLE thread)
+{
+    typedef struct _CLIENT_ID {
+        HANDLE UniqueProcess;
+        HANDLE UniqueThread;
+    } CLIENT_ID, *PCLIENT_ID;
+
+    typedef LONG NTSTATUS;
+    typedef LONG KPRIORITY;
+
+    typedef struct _THREAD_BASIC_INFORMATION {
+        NTSTATUS  ExitStatus;
+        PVOID     TebBaseAddress;
+        CLIENT_ID ClientId;
+        KAFFINITY AffinityMask;
+        KPRIORITY Priority;
+        KPRIORITY BasePriority;
+    } THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;
+
+    const static THREADINFOCLASS ThreadBasicInformation = (THREADINFOCLASS)0;
+
+    typedef NTSTATUS (WINAPI *PNtQueryInformationThread) (HANDLE thread,
+                THREADINFOCLASS infoclass, PVOID buffer, ULONG buffersize,
+                PULONG used);
+
+    static PNtQueryInformationThread NtQueryInformationThread = NULL;
+
+    THREAD_BASIC_INFORMATION tbi;
+    NTSTATUS status;
+    HMODULE  ntdll;
+    if (NtQueryInformationThread == NULL) {
+        ntdll = GetModuleHandle(L"ntdll.dll");
+        NtQueryInformationThread = (PNtQueryInformationThread)GetProcAddress(ntdll, "NtQueryInformationThread");
+        if (NtQueryInformationThread == NULL) {
+            return 0;
+        }
+    }
+
+    status = NtQueryInformationThread(thread, ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
+    if(status < 0) {
+        // Shall we go through all the trouble of setting last error?
+        return 0;
+    }
+
+    return (DWORD)tbi.ClientId.UniqueProcess;
+}