[PATCH setup 1/2] Factor out WoW detection

Jon Turney jon.turney@dronecode.org.uk
Thu Mar 19 13:52:22 GMT 2020


Factor out WoW detection as a separate function
---
 nio-ie5.cc | 45 ++++++++++++++++-----------------------------
 win32.cc   | 26 ++++++++++++++++++++++++++
 win32.h    |  2 ++
 3 files changed, 44 insertions(+), 29 deletions(-)

diff --git a/nio-ie5.cc b/nio-ie5.cc
index f5ad020..fe61b77 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -52,37 +52,24 @@ determine_default_useragent(void)
 #ifdef __x86_64__
   bitness = "Win64";
 #else
-  typedef BOOL (WINAPI *PFNISWOW64PROCESS2)(HANDLE, USHORT *, USHORT *);
-  PFNISWOW64PROCESS2 pfnIsWow64Process2 = (PFNISWOW64PROCESS2)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process2");
-
-  typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL);
-  PFNISWOW64PROCESS pfnIsWow64Process = (PFNISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
-
+  USHORT nativeMachine = WowNativeMachine();
   std::stringstream native_desc;
 
-  USHORT processMachine, nativeMachine;
-  if ((pfnIsWow64Process2) &&
-      (pfnIsWow64Process2(GetCurrentProcess(), &processMachine, &nativeMachine))) {
-    switch (nativeMachine)
-      {
-      case IMAGE_FILE_MACHINE_I386:
-        bitness = "Win32";
-        break;
-      case IMAGE_FILE_MACHINE_AMD64:
-        bitness = "WoW64";
-        break;
-      case IMAGE_FILE_MACHINE_ARM64:
-        bitness = "WoW64-ARM64";
-        break;
-      default:
-        native_desc << "WoW64-" << std::hex << nativeMachine;
-        bitness = native_desc.str();
-      }
-  } else if (pfnIsWow64Process) {
-    BOOL bIsWow64 = FALSE;
-    if (pfnIsWow64Process(GetCurrentProcess(), &bIsWow64))
-      bitness = bIsWow64 ? "WoW64" : "Win32";
-  }
+  switch (nativeMachine)
+    {
+    case IMAGE_FILE_MACHINE_I386:
+      bitness = "Win32";
+      break;
+    case IMAGE_FILE_MACHINE_AMD64:
+      bitness = "WoW64";
+      break;
+    case IMAGE_FILE_MACHINE_ARM64:
+      bitness = "WoW64-ARM64";
+      break;
+    default:
+      native_desc << "WoW64-" << std::hex << nativeMachine;
+      bitness = native_desc.str();
+    }
 #endif
   default_useragent = std::string("Cygwin-Setup/") + setup_version + " (" + os.str() + ";" + bitness + ")";
   return default_useragent;
diff --git a/win32.cc b/win32.cc
index 6a551ba..45c7bf1 100644
--- a/win32.cc
+++ b/win32.cc
@@ -393,3 +393,29 @@ VersionInfo& GetVer ()
   static VersionInfo *vi = new VersionInfo ();
   return *vi;
 }
+
+/* Identify native machine arch if we are running under WoW */
+USHORT
+WowNativeMachine ()
+{
+#ifdef __x86_64__
+  return IMAGE_FILE_MACHINE_AMD64;
+#else
+  typedef BOOL (WINAPI *PFNISWOW64PROCESS2)(HANDLE, USHORT *, USHORT *);
+  PFNISWOW64PROCESS2 pfnIsWow64Process2 = (PFNISWOW64PROCESS2)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process2");
+
+  typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL);
+  PFNISWOW64PROCESS pfnIsWow64Process = (PFNISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
+
+  USHORT processMachine, nativeMachine;
+  if ((pfnIsWow64Process2) &&
+      (pfnIsWow64Process2(GetCurrentProcess(), &processMachine, &nativeMachine)))
+    return nativeMachine;
+  else if (pfnIsWow64Process) {
+    BOOL bIsWow64 = FALSE;
+    if (pfnIsWow64Process(GetCurrentProcess(), &bIsWow64))
+      return bIsWow64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_MACHINE_I386;
+  }
+  return IMAGE_FILE_MACHINE_I386;
+#endif
+}
diff --git a/win32.h b/win32.h
index 1b9db49..a7d025d 100644
--- a/win32.h
+++ b/win32.h
@@ -177,6 +177,8 @@ VersionInfo& GetVer ();
 #define OSMinorVersion() (GetVer ().minor ())
 #define OSBuildNumber() (GetVer ().buildNumber ())
 
+USHORT WowNativeMachine ();
+
 static inline void
 GetDlgItemRect (HWND h, int item, LPRECT r)
 {
-- 
2.21.0



More information about the Cygwin-apps mailing list