From 7bd64776825f0e025cc2597e6c1909efbb1233f7 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 11 Aug 2015 19:49:37 -0700 Subject: Use the actual iphlpapi.dll and its headers The has been present since Windows 2000 and most functions we need have been present since Windows XP and Windows CE 5.0. There's no need to do runtime detection anymore. This was necessary in Qt 4.2, when this code was introduced, because we still supported Windows 98 and Me, including Windows platform SDKs that lacked the necessary header definitions. Change-Id: I076d1e8bbd53469a24fad72cf58b1905e737f52a Reviewed-by: Friedemann Kleint --- src/network/kernel/qnetworkinterface_win.cpp | 62 ++++++++++------------------ 1 file changed, 22 insertions(+), 40 deletions(-) (limited to 'src/network/kernel/qnetworkinterface_win.cpp') diff --git a/src/network/kernel/qnetworkinterface_win.cpp b/src/network/kernel/qnetworkinterface_win.cpp index dfcfdcc91f..d73d3ecdc9 100644 --- a/src/network/kernel/qnetworkinterface_win.cpp +++ b/src/network/kernel/qnetworkinterface_win.cpp @@ -31,7 +31,7 @@ ** ****************************************************************************/ -#include "qnetworkinterface_win_p.h" +#define WIN32_LEAN_AND_MEAN 1 #include "qnetworkinterface.h" #include "qnetworkinterface_p.h" @@ -41,18 +41,20 @@ #include #include #include -#include -#include +// Since we need to include winsock2.h, we need to define WIN32_LEAN_AND_MEAN +// (above) so windows.h won't include winsock.h. +// In addition, we need to include winsock2.h before iphlpapi.h and we need +// to include ws2ipdef.h to work around an MinGW-w64 bug +// (http://sourceforge.net/p/mingw-w64/mailman/message/32935366/) +#include +#include +#include +#include -QT_BEGIN_NAMESPACE +#include -typedef DWORD (WINAPI *PtrGetAdaptersInfo)(PIP_ADAPTER_INFO, PULONG); -static PtrGetAdaptersInfo ptrGetAdaptersInfo = 0; -typedef ULONG (WINAPI *PtrGetAdaptersAddresses)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG); -static PtrGetAdaptersAddresses ptrGetAdaptersAddresses = 0; -typedef DWORD (WINAPI *PtrGetNetworkParams)(PFIXED_INFO, PULONG); -static PtrGetNetworkParams ptrGetNetworkParams = 0; +QT_BEGIN_NAMESPACE static void resolveLibs() { @@ -60,20 +62,11 @@ static void resolveLibs() static bool done = false; if (!done) { - HINSTANCE iphlpapiHnd = QSystemLibrary::load(L"iphlpapi"); - if (iphlpapiHnd == NULL) { - done = true; - return; - } + HINSTANCE iphlpapiHnd = GetModuleHandle(L"iphlpapi"); + Q_ASSERT(iphlpapiHnd); #if defined(Q_OS_WINCE) - ptrGetAdaptersInfo = (PtrGetAdaptersInfo)GetProcAddress(iphlpapiHnd, L"GetAdaptersInfo"); - ptrGetAdaptersAddresses = (PtrGetAdaptersAddresses)GetProcAddress(iphlpapiHnd, L"GetAdaptersAddresses"); - ptrGetNetworkParams = (PtrGetNetworkParams)GetProcAddress(iphlpapiHnd, L"GetNetworkParams"); #else - ptrGetAdaptersInfo = (PtrGetAdaptersInfo)GetProcAddress(iphlpapiHnd, "GetAdaptersInfo"); - ptrGetAdaptersAddresses = (PtrGetAdaptersAddresses)GetProcAddress(iphlpapiHnd, "GetAdaptersAddresses"); - ptrGetNetworkParams = (PtrGetNetworkParams)GetProcAddress(iphlpapiHnd, "GetNetworkParams"); #endif done = true; } @@ -106,14 +99,14 @@ static QHash ipv4Netmasks() ULONG bufSize = sizeof staticBuf; QHash ipv4netmasks; - DWORD retval = ptrGetAdaptersInfo(pAdapter, &bufSize); + DWORD retval = GetAdaptersInfo(pAdapter, &bufSize); if (retval == ERROR_BUFFER_OVERFLOW) { // need more memory pAdapter = (IP_ADAPTER_INFO *)malloc(bufSize); if (!pAdapter) return ipv4netmasks; // try again - if (ptrGetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) { + if (GetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) { free(pAdapter); return ipv4netmasks; } @@ -148,14 +141,14 @@ static QList interfaceListingWinXP() ULONG flags = GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_MULTICAST; - ULONG retval = ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize); + ULONG retval = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize); if (retval == ERROR_BUFFER_OVERFLOW) { // need more memory pAdapter = (IP_ADAPTER_ADDRESSES *)malloc(bufSize); if (!pAdapter) return interfaces; // try again - if (ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) { + if (GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) { free(pAdapter); return interfaces; } @@ -224,36 +217,25 @@ static QList interfaceListingWinXP() return interfaces; } -static QList interfaceListing() -{ - resolveLibs(); - if (ptrGetAdaptersAddresses != NULL) - return interfaceListingWinXP(); - - // failed - return QList(); -} - QList QNetworkInterfaceManager::scan() { - return interfaceListing(); + resolveLibs(); + return interfaceListingWinXP(); } QString QHostInfo::localDomainName() { resolveLibs(); - if (ptrGetNetworkParams == NULL) - return QString(); // couldn't resolve FIXED_INFO info, *pinfo; ULONG bufSize = sizeof info; pinfo = &info; - if (ptrGetNetworkParams(pinfo, &bufSize) == ERROR_BUFFER_OVERFLOW) { + if (GetNetworkParams(pinfo, &bufSize) == ERROR_BUFFER_OVERFLOW) { pinfo = (FIXED_INFO *)malloc(bufSize); if (!pinfo) return QString(); // try again - if (ptrGetNetworkParams(pinfo, &bufSize) != ERROR_SUCCESS) { + if (GetNetworkParams(pinfo, &bufSize) != ERROR_SUCCESS) { free(pinfo); return QString(); // error } -- cgit v1.2.3