diff --git a/evdns.c b/evdns.c index e517895803..c861daa3bb 100644 --- a/evdns.c +++ b/evdns.c @@ -3509,7 +3509,8 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base) GetNetworkParams_fn_t fn; ASSERT_LOCKED(base); - if (!(handle = LoadLibrary(TEXT("iphlpapi.dll")))) { + if (!(handle = evutil_load_windows_system_library( + TEXT("iphlpapi.dll")))) { log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll"); status = -1; goto done; diff --git a/evutil.c b/evutil.c index adcb4db4ae..794ad1c0d8 100644 --- a/evutil.c +++ b/evutil.c @@ -35,6 +35,7 @@ #include #undef WIN32_LEAN_AND_MEAN #include +#include #endif #include @@ -2060,3 +2061,19 @@ evutil_hex_char_to_int(char c) } return -1; } + +#ifdef WIN32 +HANDLE +evutil_load_windows_system_library(const TCHAR *library_name) +{ + TCHAR path[MAX_PATH]; + unsigned n; + n = GetSystemDirectory(path, MAX_PATH); + 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/test/regress_util.c b/test/regress_util.c index 9babc0a934..4eea2e9c8f 100644 --- a/test/regress_util.c +++ b/test/regress_util.c @@ -1037,6 +1037,22 @@ test_evutil_getaddrinfo(void *arg) evutil_freeaddrinfo(ai); } +#ifdef WIN32 +static void +test_evutil_loadsyslib(void *arg) +{ + HANDLE h=NULL; + + h = evutil_load_windows_system_library(TEXT("kernel32.dll")); + tt_assert(h); + +end: + if (h) + CloseHandle(h); + +} +#endif + struct testcase_t util_testcases[] = { { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, @@ -1052,6 +1068,9 @@ struct testcase_t util_testcases[] = { { "integers", test_evutil_integers, 0, NULL, NULL }, { "rand", test_evutil_rand, TT_FORK, NULL, NULL }, { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL }, +#ifdef WIN32 + { "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL }, +#endif END_OF_TESTCASES, }; diff --git a/util-internal.h b/util-internal.h index e7245b6930..e318ac9afc 100644 --- a/util-internal.h +++ b/util-internal.h @@ -224,6 +224,10 @@ long evutil_tv_to_msec(const struct timeval *tv); int evutil_hex_char_to_int(char c); +#ifdef WIN32 +HANDLE evutil_load_windows_system_library(const TCHAR *library_name); +#endif + #ifdef __cplusplus } #endif