-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Windows Version Helper functions
- Loading branch information
Showing
4 changed files
with
368 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
contrib/platform/src/com/sun/jna/platform/win32/VersionHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
/* Copyright (c) 2019 Daniel Widdis, All Rights Reserved | ||
* | ||
* The contents of this file is dual-licensed under 2 | ||
* alternative Open Source/Free licenses: LGPL 2.1 or later and | ||
* Apache License 2.0. (starting with JNA version 4.0.0). | ||
* | ||
* You can freely decide which license you want to apply to | ||
* the project. | ||
* | ||
* You may obtain a copy of the LGPL License at: | ||
* | ||
* http://www.gnu.org/licenses/licenses.html | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "LGPL2.1". | ||
* | ||
* You may obtain a copy of the Apache License at: | ||
* | ||
* http://www.apache.org/licenses/ | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "AL2.0". | ||
*/ | ||
package com.sun.jna.platform.win32; | ||
|
||
import com.sun.jna.platform.win32.WinDef.DWORD; | ||
import com.sun.jna.platform.win32.WinDef.WORD; | ||
import com.sun.jna.platform.win32.WinNT.OSVERSIONINFOEX; | ||
|
||
/** | ||
* The following functions can be used to determine the current operating system | ||
* version or identify whether it is a Windows or Windows Server release. These | ||
* functions provide simple tests that use the VerifyVersionInfo function and | ||
* the recommended greater than or equal to comparisons that are proven as a | ||
* robust means to determine the operating system version. | ||
*/ | ||
public class VersionHelpers { | ||
|
||
/** | ||
* This function is useful in confirming a version of Windows Server that | ||
* doesn't share a version number with a client release. You should only use | ||
* this function if the other provided version helper functions do not fit | ||
* your scenario. | ||
* | ||
* @param wMajorVersion | ||
* The major version to test | ||
* @param wMinorVersion | ||
* The minor version to test | ||
* @param wServicePackMajor | ||
* The service pack to test | ||
* @return True if the current OS version matches, or is greater than, the | ||
* provided version information. | ||
*/ | ||
public static boolean IsWindowsVersionOrGreater(int wMajorVersion, int wMinorVersion, int wServicePackMajor) { | ||
OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); | ||
osvi.dwOSVersionInfoSize = new DWORD(osvi.size()); | ||
osvi.dwMajorVersion = new DWORD(wMajorVersion); | ||
osvi.dwMinorVersion = new DWORD(wMinorVersion); | ||
osvi.wServicePackMajor = new WORD(wServicePackMajor); | ||
|
||
long dwlConditionMask = 0; | ||
dwlConditionMask = Kernel32.INSTANCE.VerSetConditionMask(dwlConditionMask, WinNT.VER_MAJORVERSION, | ||
WinNT.VER_GREATER_EQUAL); | ||
dwlConditionMask = Kernel32.INSTANCE.VerSetConditionMask(dwlConditionMask, WinNT.VER_MINORVERSION, | ||
WinNT.VER_GREATER_EQUAL); | ||
dwlConditionMask = Kernel32.INSTANCE.VerSetConditionMask(dwlConditionMask, WinNT.VER_SERVICEPACKMAJOR, | ||
WinNT.VER_GREATER_EQUAL); | ||
|
||
return Kernel32.INSTANCE.VerifyVersionInfoW(osvi, | ||
WinNT.VER_MAJORVERSION | WinNT.VER_MINORVERSION | WinNT.VER_SERVICEPACKMAJOR, dwlConditionMask); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows XP version. | ||
*/ | ||
public static boolean IsWindowsXPOrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WINXP), LOBYTE(WinNT.WIN32_WINNT_WINXP), 0); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows XP with Service Pack 1 (SP1) version. | ||
*/ | ||
public static boolean IsWindowsXPSP1OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WINXP), LOBYTE(WinNT.WIN32_WINNT_WINXP), 1); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows XP with Service Pack 2 (SP2) version. | ||
*/ | ||
public static boolean IsWindowsXPSP2OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WINXP), LOBYTE(WinNT.WIN32_WINNT_WINXP), 2); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows XP with Service Pack 3 (SP3) version. | ||
*/ | ||
public static boolean IsWindowsXPSP3OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WINXP), LOBYTE(WinNT.WIN32_WINNT_WINXP), 3); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows Vista version. | ||
*/ | ||
public static boolean IsWindowsVistaOrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_VISTA), LOBYTE(WinNT.WIN32_WINNT_VISTA), 0); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows Vista with Service Pack 1 (SP1) version. | ||
*/ | ||
public static boolean IsWindowsVistaSP1OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_VISTA), LOBYTE(WinNT.WIN32_WINNT_VISTA), 1); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows Vista with Service Pack 2 (SP2) version. | ||
*/ | ||
public static boolean IsWindowsVistaSP2OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_VISTA), LOBYTE(WinNT.WIN32_WINNT_VISTA), 2); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows 7 version. | ||
*/ | ||
public static boolean IsWindows7OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WIN7), LOBYTE(WinNT.WIN32_WINNT_WIN7), 0); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows 7 with Service Pack 1 (SP1) version. | ||
*/ | ||
public static boolean IsWindows7SP1OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WIN7), LOBYTE(WinNT.WIN32_WINNT_WIN7), 1); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows 8 version. | ||
*/ | ||
public static boolean IsWindows8OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WIN8), LOBYTE(WinNT.WIN32_WINNT_WIN8), 0); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows 8.1 version. For Windows 10, IsWindows8Point1OrGreater | ||
* returns false unless the application contains a manifest that | ||
* includes a compatibility section that contains the GUIDs that | ||
* designate Windows 8.1 and/or Windows 10. | ||
*/ | ||
public static boolean IsWindows8Point1OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WINBLUE), LOBYTE(WinNT.WIN32_WINNT_WINBLUE), 0); | ||
} | ||
|
||
/** | ||
* @return true if the current OS version matches, or is greater than, the | ||
* Windows 10 version. For Windows 10, IsWindows10OrGreater returns | ||
* false unless the application contains a manifest that includes a | ||
* compatibility section that contains the GUID that designates | ||
* Windows 10. | ||
*/ | ||
public static boolean IsWindows10OrGreater() { | ||
return IsWindowsVersionOrGreater(HIBYTE(WinNT.WIN32_WINNT_WIN10), LOBYTE(WinNT.WIN32_WINNT_WIN10), 0); | ||
} | ||
|
||
/** | ||
* Applications that need to distinguish between server and client versions | ||
* of Windows should call this function. | ||
* | ||
* @return true if the current OS is a Windows Server release. | ||
*/ | ||
public static boolean IsWindowsServer() { | ||
OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); | ||
osvi.dwOSVersionInfoSize = new DWORD(osvi.size()); | ||
osvi.wProductType = WinNT.VER_NT_WORKSTATION; | ||
|
||
long dwlConditionMask = Kernel32.INSTANCE.VerSetConditionMask(0, WinNT.VER_PRODUCT_TYPE, WinNT.VER_EQUAL); | ||
|
||
return !Kernel32.INSTANCE.VerifyVersionInfoW(osvi, WinNT.VER_PRODUCT_TYPE, dwlConditionMask); | ||
} | ||
|
||
/** | ||
* Get the high byte | ||
* | ||
* @param word | ||
* a two-byte value | ||
* @return The most significant byte | ||
*/ | ||
private static byte HIBYTE(short word) { | ||
return (byte) ((word >> 8) & 0xFF); | ||
} | ||
|
||
/** | ||
* Get the low byte | ||
* | ||
* @param word | ||
* a two-byte value | ||
* @return The least significant byte | ||
*/ | ||
private static byte LOBYTE(short word) { | ||
return (byte) word; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
contrib/platform/test/com/sun/jna/platform/win32/VersionHelpersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.sun.jna.platform.win32; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
public class VersionHelpersTest { | ||
@Test | ||
public void testVersionHelpers() { | ||
// All windows versions should be higher than version 0.0! | ||
assertTrue(VersionHelpers.IsWindowsVersionOrGreater(0, 0, 0)); | ||
// All windows versions should be lower than version Short.MAX_VALUE! | ||
assertFalse(VersionHelpers.IsWindowsVersionOrGreater(Short.MAX_VALUE, Short.MAX_VALUE, Short.MAX_VALUE)); | ||
// These tests in order should be true until false; once false never | ||
// true again | ||
boolean lastVersionTest = true; | ||
boolean versionTest = VersionHelpers.IsWindowsXPOrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindowsXPSP1OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindowsXPSP2OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindowsXPSP3OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindowsVistaOrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindowsVistaSP1OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindowsVistaSP2OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindows7OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindows7SP1OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindows8OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindows8Point1OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
|
||
lastVersionTest = versionTest; | ||
versionTest = VersionHelpers.IsWindows10OrGreater(); | ||
assertTrue((lastVersionTest == versionTest) || !versionTest); | ||
} | ||
} | ||
|