git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4] mingw: prefer RtlGetVersion over GetVersion

From
RGRose via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 21, 2023, 20:04 UTC
Message-ID
<pull.1438.v4.git.git.1674331462560.gitgitgadget@gmail.com>
In-Reply-To
<pull.1438.v3.git.git.1674330625069.gitgitgadget@gmail.com>
From: Seija Kijin <doremylover123@gmail.com>

The previous way is deprecated and returns the wrong value in Windows 8 and up, returning the manifest Windows data as opposed to the actual Windows data.

RtlGetVersion is the correct way to get the Windows version now.

Note: ntdll does not need to be
manually loaded into the runtime,
as this is the one special library
that is automatically loaded upon launch.
Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
    mingw: prefer RtlGetVersion over GetVersion
    
    GetVersion has its behavior changed in Windows 8 and above anyway, so
    this is the right way to do it now.
    
    The previous way returns the wrong value in Windows 8 and up, returning
    the manifest Windows data as opposed to the actual Windows data.
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1438%2FAtariDreams%2Fmingw-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1438/AtariDreams/mingw-v4
Pull-Request: https://github.com/git/git/pull/1438
Range-diff vs v3:
 1:  31f778a6b34 ! 1:  8293e868970 mingw: replace deprecated GetVersion with RtlGetVersion
     @@ Metadata
      Author: Seija Kijin <doremylover123@gmail.com>
      
       ## Commit message ##
     -    mingw: replace deprecated GetVersion with RtlGetVersion
     +    mingw: prefer RtlGetVersion over GetVersion
      
          The previous way is deprecated and returns
          the wrong value in Windows 8 and up,
 compat/mingw.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index af397e68a1d..b1d75c93cfe 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -3081,15 +3081,42 @@ int wmain(int argc, const wchar_t **wargv)
 	return exit_status;
 }
 
+/*
+ * for RtlGetVersion in uname
+ */
+
+typedef NTSTATUS(WINAPI *RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
+union winprocaddr {
+	FARPROC procaddr;
+	RtlGetVersionPtr procGetVersion;
+};
+
 int uname(struct utsname *buf)
 {
-	unsigned v = (unsigned)GetVersion();
+	union winprocaddr RtlGetVersionInternal;
+	OSVERSIONINFOW version;
+
+	memset(&version, 0, sizeof(version));
+	version.dwOSVersionInfoSize = sizeof(version);
+
+	/* RtlGetVersion always gets the true Windows version, even when running
+	 * under Windows's compatibility mode*/
+	RtlGetVersionInternal.procaddr =
+		GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion");
+
+	if (RtlGetVersionInternal.procaddr) {
+		RtlGetVersionInternal.procGetVersion((PRTL_OSVERSIONINFOW)&version);
+	} else {
+		/* Should not happen, but just in case, fallback to deprecated
+		 * GetVersionExW */
+		GetVersionExW(&version);
+	}
+
 	memset(buf, 0, sizeof(*buf));
 	xsnprintf(buf->sysname, sizeof(buf->sysname), "Windows");
-	xsnprintf(buf->release, sizeof(buf->release),
-		 "%u.%u", v & 0xff, (v >> 8) & 0xff);
-	/* assuming NT variants only.. */
-	xsnprintf(buf->version, sizeof(buf->version),
-		  "%u", (v >> 16) & 0x7fff);
+	xsnprintf(buf->release, sizeof(buf->release), "%lu.%lu",
+		  version.dwMajorVersion, version.dwMinorVersion);
+	xsnprintf(buf->version, sizeof(buf->version), "%lu",
+		  version.dwBuildNumber);
 	return 0;
 }

base-commit: 904d404274fef6695c78a6b055edd184b72e2f9b
-- 
gitgitgadget
Previous: Rose via GitGitGadgetNext: Johannes Schindelin
Message 4 of 5 in “mingw: replace deprecated GetVersion with RtlGetVersion”
  1. mingw: replace deprecated GetVersion with RtlGetVersionRose via GitGitGadget, Jan 19, 2023
  2. mingw: replace deprecated GetVersion with RtlGetVersionRose via GitGitGadget, Jan 19, 2023
  3. mingw: replace deprecated GetVersion with RtlGetVersionRose via GitGitGadget, Jan 21, 2023
  4. mingw: prefer RtlGetVersion over GetVersionRose via GitGitGadget, Jan 21, 2023
  5. Johannes SchindelinMar 27, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.