[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1870: Load tlhelp32 functions dynamically so we can run on Windows (trunk/src/util)



Author: edmanm
Date: 2007-08-25 00:02:53 -0400 (Sat, 25 Aug 2007)
New Revision: 1870

Modified:
   trunk/src/util/win32.cpp
Log:
Load tlhelp32 functions dynamically so we can run on Windows NT 4.0.


Modified: trunk/src/util/win32.cpp
===================================================================
--- trunk/src/util/win32.cpp	2007-08-25 02:28:42 UTC (rev 1869)
+++ trunk/src/util/win32.cpp	2007-08-25 04:02:53 UTC (rev 1870)
@@ -29,8 +29,25 @@
 #include <tlhelp32.h>
 #include <shlobj.h>
 #include <QDir>
+#include <QLibrary>
+#include <QtDebug>
 
+#if defined(UNICODE)
+/* Force the ascii verisons of these functions, so we can run on Win98. We
+ * don't pass any Unicode strings to these functions anyway. */
+#undef PROCESSENTRY32
+#undef LPPROCESSENTRY32
+#undef Process32First
+#undef Process32Next
+#endif
 
+/* Load the tool help functions dynamically, since they don't exist on
+ * Windows NT 4.0 */
+typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_fn)(DWORD, DWORD);
+typedef BOOL (WINAPI *Process32First_fn)(HANDLE, LPPROCESSENTRY32);
+typedef BOOL (WINAPI *Process32Next_fn)(HANDLE, LPPROCESSENTRY32);
+
+
 /** Finds the location of the "special" Windows folder using the given CSIDL
  * value. If the folder cannot be found, the given default path is used. */
 QString
@@ -149,26 +166,34 @@
 QHash<qint64, QString>
 win32_process_list()
 {
-#if defined(UNICODE)
-/* Force the ascii verisons of these functions, so we can run on Win98. We
- * don't pass any Unicode strings to these functions anyway. */
-#undef PROCESSENTRY32
-#undef Process32First
-#undef Process32Next
-#endif
   QHash<qint64, QString> procList;
+  CreateToolhelp32Snapshot_fn pCreateToolhelp32Snapshot;
+  Process32First_fn pProcess32First;
+  Process32Next_fn pProcess32Next;
   HANDLE hSnapshot;
   PROCESSENTRY32 proc;
   QString exeFile;
   qint64 pid;
 
+  /* Load the tool help functions */
+  pCreateToolhelp32Snapshot =
+    (CreateToolhelp32Snapshot_fn)QLibrary::resolve("kernel32", "CreateToolhelp32Snapshot");
+  pProcess32First = (Process32First_fn)QLibrary::resolve("kernel32", "Process32First");
+  pProcess32Next = (Process32Next_fn)QLibrary::resolve("kernel32", "Process32Next");
+ 
+  if (!pCreateToolhelp32Snapshot || !pProcess32First || !pProcess32Next) {
+    qWarning("Unable to load tool help functions. Running process information "
+             "will be unavailable.");
+    return QHash<qint64, QString>();
+  }
+
   /* Create a snapshot of all active processes */
-  hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+  hSnapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   if (hSnapshot != INVALID_HANDLE_VALUE) {
     proc.dwSize = sizeof(PROCESSENTRY32);
     
     /* Iterate through all the processes in the snapshot */
-    if (Process32First(hSnapshot, &proc)) {
+    if (pProcess32First(hSnapshot, &proc)) {
       do {
         /* Extract the PID and exe filename from the process record */
         pid = (qint64)proc.th32ProcessID;
@@ -176,7 +201,7 @@
         
         /* Add this process to our list */
         procList.insert(pid, exeFile);
-      } while (Process32Next(hSnapshot, &proc));
+      } while (pProcess32Next(hSnapshot, &proc));
     }
     CloseHandle(hSnapshot);
   }