fb9ae34
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
fb9ae34
index 83548d1..d9f80eb 100644
fb9ae34
--- a/src/CMakeLists.txt
fb9ae34
+++ b/src/CMakeLists.txt
fb9ae34
@@ -4,6 +4,29 @@ MESSAGE(STATUS "CMAKE_SYSTEM_NAME is ${CMAKE_SYSTEM_NAME}")
fb9ae34
 
fb9ae34
 INCLUDE_DIRECTORIES(../include/)
fb9ae34
 
fb9ae34
+INCLUDE(CheckCSourceCompiles)
fb9ae34
+
fb9ae34
+MACRO (CHECK_STRUCT_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
fb9ae34
+   SET(_INCLUDE_FILES)
fb9ae34
+   FOREACH (it ${_HEADER})
fb9ae34
+      SET(_INCLUDE_FILES "${_INCLUDE_FILES}#include <${it}>\n")
fb9ae34
+   ENDFOREACH (it)
fb9ae34
+
fb9ae34
+   SET(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
fb9ae34
+${_INCLUDE_FILES}
fb9ae34
+int main()
fb9ae34
+{
fb9ae34
+   static ${_STRUCT} tmp;
fb9ae34
+   if (sizeof(tmp.${_MEMBER}))
fb9ae34
+      return 0;
fb9ae34
+  return 0;
fb9ae34
+}
fb9ae34
+")
fb9ae34
+   CHECK_C_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
fb9ae34
+
fb9ae34
+ENDMACRO (CHECK_STRUCT_MEMBER)
fb9ae34
+
fb9ae34
+
fb9ae34
 ## linux
fb9ae34
 IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
fb9ae34
   SET(SIGAR_SRC os/linux/linux_sigar.c)
fb9ae34
@@ -60,6 +83,8 @@ IF(WIN32)
fb9ae34
   ADD_DEFINITIONS(-DSIGAR_SHARED)
fb9ae34
   SET(SIGAR_SRC os/win32/peb.c os/win32/win32_sigar.c)
fb9ae34
   INCLUDE_DIRECTORIES(os/win32)
fb9ae34
+  CHECK_STRUCT_MEMBER(MIB_IPADDRROW wType "windows.h;iphlpapi.h" wType_in_MIB_IPADDRROW)
fb9ae34
+  add_definitions(-DHAVE_MIB_IPADDRROW_WTYPE=${wType_in_MIB_IPADDRROW})
fb9ae34
 ENDIF(WIN32)
fb9ae34
 
fb9ae34
 SET(SIGAR_SRC ${SIGAR_SRC}
fb9ae34
diff --git a/src/os/win32/sigar_os.h b/src/os/win32/sigar_os.h
fb9ae34
index ff2de7c..cd3c7e8 100755
fb9ae34
--- a/src/os/win32/sigar_os.h
fb9ae34
+++ b/src/os/win32/sigar_os.h
fb9ae34
@@ -19,11 +19,15 @@
fb9ae34
 #ifndef SIGAR_OS_H
fb9ae34
 #define SIGAR_OS_H
fb9ae34
 
fb9ae34
+#ifdef MSVC
fb9ae34
 #if _MSC_VER <= 1200
fb9ae34
 #define SIGAR_USING_MSC6 /* Visual Studio version 6 */
fb9ae34
 #endif
fb9ae34
+#else
fb9ae34
+/* Cross compiling */
fb9ae34
+#define _WIN32_WINNT 0x0501
fb9ae34
+#endif
fb9ae34
 
fb9ae34
-#define WIN32_LEAN_AND_MEAN
fb9ae34
 #include <windows.h>
fb9ae34
 #include <winreg.h>
fb9ae34
 #include <winperf.h>
fb9ae34
@@ -38,7 +42,22 @@
fb9ae34
 
fb9ae34
 #include "sigar_util.h"
fb9ae34
 
fb9ae34
-#define INT64_C(val) val##i64
fb9ae34
+#ifdef MSVC
fb9ae34
+#  define INT64_C(val) val##i64
fb9ae34
+#  define SIGAR_DLLFUNC(api, name) \
fb9ae34
+    struct { \
fb9ae34
+         const char *name; \
fb9ae34
+         ##api##_##name func; \
fb9ae34
+    } ##name
fb9ae34
+#else
fb9ae34
+/* The GCC compiler doesn't require/accept the ## prefix */
fb9ae34
+#  define INT64_C(val) val##L
fb9ae34
+#  define SIGAR_DLLFUNC(api, name)		\
fb9ae34
+    struct { \
fb9ae34
+         const char *name; \
fb9ae34
+         api##_##name func; \
fb9ae34
+    } name
fb9ae34
+#endif
fb9ae34
 
fb9ae34
 /* see apr/include/arch/win32/atime.h */
fb9ae34
 #define EPOCH_DELTA INT64_C(11644473600000000)
fb9ae34
@@ -457,12 +476,6 @@ typedef BOOL (CALLBACK *mpr_get_net_connection)(LPCTSTR,
fb9ae34
                                                 LPTSTR,
fb9ae34
                                                 LPDWORD);
fb9ae34
 
fb9ae34
-#define SIGAR_DLLFUNC(api, name) \
fb9ae34
-    struct { \
fb9ae34
-         const char *name; \
fb9ae34
-         ##api##_##name func; \
fb9ae34
-    } ##name
fb9ae34
-
fb9ae34
 typedef struct {
fb9ae34
     sigar_dll_handle_t handle;
fb9ae34
 
fb9ae34
diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c
fb9ae34
index b9a5669..74bf874 100755
fb9ae34
--- a/src/os/win32/win32_sigar.c
fb9ae34
+++ b/src/os/win32/win32_sigar.c
fb9ae34
@@ -23,6 +23,7 @@
fb9ae34
 #include "sigar_util.h"
fb9ae34
 #include "sigar_format.h"
fb9ae34
 #include <shellapi.h>
fb9ae34
+#include <iphlpapi.h>
fb9ae34
 
fb9ae34
 #define USING_WIDE_S(s) (s)->using_wide
fb9ae34
 #define USING_WIDE()    USING_WIDE_S(sigar)
fb9ae34
@@ -124,6 +125,9 @@ typedef enum {
fb9ae34
 #define MS_LOOPBACK_ADAPTER "Microsoft Loopback Adapter"
fb9ae34
 #define NETIF_LA "la"
fb9ae34
 
fb9ae34
+static int get_proc_info(sigar_t *sigar, sigar_pid_t pid);
fb9ae34
+static int netif_hash(char *s);
fb9ae34
+
fb9ae34
 sigar_uint64_t sigar_FileTimeToTime(FILETIME *ft)
fb9ae34
 {
fb9ae34
     sigar_uint64_t time;
fb9ae34
@@ -374,6 +378,7 @@ static sigar_mpr_t sigar_mpr = {
fb9ae34
     { NULL, NULL }
fb9ae34
 };
fb9ae34
 
fb9ae34
+#ifdef MSVC
fb9ae34
 #define DLLMOD_COPY(name) \
fb9ae34
     memcpy(&(sigar->##name), &sigar_##name, sizeof(sigar_##name))
fb9ae34
 
fb9ae34
@@ -382,6 +387,18 @@ static sigar_mpr_t sigar_mpr = {
fb9ae34
 
fb9ae34
 #define DLLMOD_FREE(name) \
fb9ae34
     sigar_dllmod_free((sigar_dll_module_t *)&(sigar->##name))
fb9ae34
+#else
fb9ae34
+/* The GCC compiler doesn't require/accept the ## prefix */
fb9ae34
+#define DLLMOD_COPY(name) \
fb9ae34
+    memcpy(&(sigar->name), &sigar_##name, sizeof(sigar_##name))
fb9ae34
+
fb9ae34
+#define DLLMOD_INIT(name, all) \
fb9ae34
+    sigar_dllmod_init(sigar, (sigar_dll_module_t *)&(sigar->name), all)
fb9ae34
+
fb9ae34
+#define DLLMOD_FREE(name) \
fb9ae34
+    sigar_dllmod_free((sigar_dll_module_t *)&(sigar->name))
fb9ae34
+#endif
fb9ae34
+
fb9ae34
 
fb9ae34
 static void sigar_dllmod_free(sigar_dll_module_t *module)
fb9ae34
 {
fb9ae34
@@ -1470,7 +1487,9 @@ static int sigar_remote_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
fb9ae34
     }
fb9ae34
 
fb9ae34
     /* likely we are 32-bit, pid process is 64-bit */
fb9ae34
+#ifdef MSVC
fb9ae34
     status = sigar_proc_args_wmi_get(sigar, pid, procargs);
fb9ae34
+#endif
fb9ae34
     if (status == ERROR_NOT_FOUND) {
fb9ae34
         status = SIGAR_NO_SUCH_PROCESS;
fb9ae34
     }
fb9ae34
@@ -1507,7 +1526,7 @@ static int sigar_proc_env_parse(UCHAR *ptr, sigar_proc_env_t *procenv,
fb9ae34
             break; /*XXX*/
fb9ae34
         }
fb9ae34
 
fb9ae34
-        klen = val - ptr;
fb9ae34
+        klen = val - (char*)ptr;
fb9ae34
         SIGAR_SSTRCPY(key, ptr);
fb9ae34
         key[klen] = '\0';
fb9ae34
         ++val;
fb9ae34
@@ -1640,6 +1659,7 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
fb9ae34
     }
fb9ae34
 
fb9ae34
     status = sigar_proc_exe_peb_get(sigar, proc, procexe);
fb9ae34
+#ifdef MSVC
fb9ae34
     if (procexe->name[0] == '\0') {
fb9ae34
         /* likely we are 32-bit, pid process is 64-bit */
fb9ae34
         /* procexe->cwd[0] = XXX where else can we try? */
fb9ae34
@@ -1648,7 +1668,7 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
fb9ae34
             status = SIGAR_NO_SUCH_PROCESS;
fb9ae34
         }
fb9ae34
     }
fb9ae34
-
fb9ae34
+#endif
fb9ae34
     if (procexe->cwd[0] != '\0') {
fb9ae34
         /* strip trailing '\' */
fb9ae34
         int len = strlen(procexe->cwd);
fb9ae34
@@ -2394,10 +2414,10 @@ static int sigar_get_netif_ipaddr(sigar_t *sigar,
fb9ae34
             MIB_IPADDRROW *row = &mib->table[i];
fb9ae34
             short type;
fb9ae34
 
fb9ae34
-#ifdef SIGAR_USING_MSC6
fb9ae34
-            type = row->unused2;
fb9ae34
-#else
fb9ae34
+#if HAVE_MIB_IPADDRROW_WTYPE
fb9ae34
             type = row->wType;
fb9ae34
+#else
fb9ae34
+            type = row->unused2;
fb9ae34
 #endif
fb9ae34
             if (!(type & MIB_IPADDR_PRIMARY)) {
fb9ae34
                 continue;
fb9ae34
diff --git a/tests/t_sigar_fs.c b/tests/t_sigar_fs.c
fb9ae34
index f298a08..7a554de 100644
fb9ae34
--- a/tests/t_sigar_fs.c
fb9ae34
+++ b/tests/t_sigar_fs.c
fb9ae34
@@ -39,7 +39,7 @@
fb9ae34
 #include <stdio.h>
fb9ae34
 #include <string.h>
fb9ae34
 #include <errno.h>
fb9ae34
-#if defined(_WIN32)
fb9ae34
+#if defined(MSVC)
fb9ae34
 #include <WinError.h>
fb9ae34
 #endif
fb9ae34
 
fb9ae34
@@ -85,7 +85,7 @@ TEST(test_sigar_file_system_list_get) {
fb9ae34
 		} else {
fb9ae34
 			switch (ret) {
fb9ae34
 				/* track the expected error code */
fb9ae34
-#if defined(_WIN32)
fb9ae34
+#if defined(MSVC)
fb9ae34
 			case ERROR_NOT_READY:
fb9ae34
 				break;
fb9ae34
 #endif
fb9ae34
diff --git a/tests/t_sigar_proc.c b/tests/t_sigar_proc.c
fb9ae34
index 88a9ac7..334bae3 100644
fb9ae34
--- a/tests/t_sigar_proc.c
fb9ae34
+++ b/tests/t_sigar_proc.c
fb9ae34
@@ -39,7 +39,7 @@
fb9ae34
 #include <stdio.h>
fb9ae34
 #include <string.h>
fb9ae34
 #include <errno.h>
fb9ae34
-#if defined(_WIN32)
fb9ae34
+#if defined(MSVC)
fb9ae34
 #include <WinError.h>
fb9ae34
 #endif
fb9ae34
 
fb9ae34
@@ -127,7 +127,7 @@ TEST(test_sigar_proc_list_get) {
fb9ae34
 			switch (ret) {
fb9ae34
 			case EPERM:
fb9ae34
 			case ESRCH:
fb9ae34
-#if (defined(_WIN32))
fb9ae34
+#if (defined(MSVC))
fb9ae34
 			/* OpenProcess() may return ERROR_ACCESS_DENIED */
fb9ae34
 			case ERROR_ACCESS_DENIED:
fb9ae34
 #endif