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

[or-cvs] [tor/maint-0.2.2 1/3] New function to load windows system libraries



Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date: Tue, 21 Sep 2010 13:07:11 -0400
Subject: New function to load windows system libraries
Commit: 418e6caeebfb13c66ea1b4904fb331fe57b82b80

This function uses GetSystemDirectory() to make sure we load the version
of the library from c:\windows\system32 (or local equivalent) rather than
whatever version lives in the cwd.
---
 src/common/util.c    |   15 +++++++++++++++
 src/common/util.h    |    4 ++++
 src/test/test_util.c |   16 ++++++++++++++++
 3 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/src/common/util.c b/src/common/util.c
index 1d77045..12be008 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -26,6 +26,7 @@
 #include <io.h>
 #include <direct.h>
 #include <process.h>
+#include <tchar.h>
 #else
 #include <dirent.h>
 #include <pwd.h>
@@ -2793,3 +2794,17 @@ write_pidfile(char *filename)
   }
 }
 
+#ifdef MS_WINDOWS
+HANDLE
+load_windows_system_library(const TCHAR *library_name)
+{
+  TCHAR path[MAX_PATH];
+  unsigned n;
+  n = GetSystemDirectory(path, 1024);
+  if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
+    return 0;
+  _tcscat(path, TEXT("\\"));
+  _tcscat(path, library_name);
+  return LoadLibrary(path);
+}
+#endif
diff --git a/src/common/util.h b/src/common/util.h
index 4a31c27..833fd90 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -340,6 +340,10 @@ void start_daemon(void);
 void finish_daemon(const char *desired_cwd);
 void write_pidfile(char *filename);
 
+#ifdef MS_WINDOWS
+HANDLE load_windows_system_library(const TCHAR *library_name);
+#endif
+
 const char *libor_get_digests(void);
 
 #endif
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 8a13597..116d4f5 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1139,6 +1139,19 @@ test_util_listdir(void *ptr)
   }
 }
 
+#ifdef MS_WINDOWS
+static void
+test_util_load_win_lib(void *ptr)
+{
+  HANDLE h = load_windows_system_library("advapi32.dll");
+
+  tt_assert(h);
+ done:
+  if (h)
+    CloseHandle(h);
+}
+#endif
+
 #define UTIL_LEGACY(name)                                               \
   { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
 
@@ -1162,6 +1175,9 @@ struct testcase_t util_tests[] = {
   UTIL_TEST(find_str_at_start_of_line, 0),
   UTIL_TEST(asprintf, 0),
   UTIL_TEST(listdir, 0),
+#ifdef MS_WINDOWS
+  UTIL_TEST(load_win_lib, 0),
+#endif
   END_OF_TESTCASES
 };
 
-- 
1.7.1