static bool IsWindowsServer() { try { const int OS_ANYSERVER = 29; return(IsOS(OS_ANYSERVER)); } catch { // isn't supported in nano server } try { const uint VER_PRODUCT_TYPE = 0x0000080; const byte VER_EQUAL = 1; const byte VER_NT_WORKSTATION = 0x0000001; var osvi = new OSVERSIONINFOEXW { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEXW)), wProductType = VER_NT_WORKSTATION }; var dwlConditionMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL); var result = VerifyVersionInfoW(ref osvi, VER_PRODUCT_TYPE, dwlConditionMask) == false; return(result); } catch { // failed to identify if running in Windows Server. assuming that we aren't. return(false); } }
public static bool RtlGetVersion(ref OSVERSIONINFOEXW lpVersionInformation) { IntPtr proc = GetProcAddress(GetNtDll(), "RtlGetVersion"); NativeSysCall.Delegates.RtlGetVersion RtlGetVersion = (NativeSysCall.Delegates.RtlGetVersion)Marshal.GetDelegateForFunctionPointer(proc, typeof(NativeSysCall.Delegates.RtlGetVersion)); return(RtlGetVersion(ref lpVersionInformation)); }
static Version GetRtlVersion() { OSVERSIONINFOEXW info = new OSVERSIONINFOEXW(); info.dwOSVersionInfoSize = Marshal.SizeOf(info); RtlGetVersion(ref info); return(new Version(info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber)); }
IsWindowsServer() { var osvi = new OSVERSIONINFOEXW { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEXW)), wProductType = VER_NT_WORKSTATION }; var dwlConditionMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL); return(!VerifyVersionInfoW(ref osvi, VER_PRODUCT_TYPE, dwlConditionMask)); }
public static bool IsWindowsVersionOrGreater(int majorVersion, int minorVersion, short servicePackMajor) { var osvi = new OSVERSIONINFOEXW(); osvi.dwOSVersionInfoSize = Marshal.SizeOf <OSVERSIONINFOEXW>(); var mask = VerSetConditionMask(VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL), VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); osvi.dwMajorVersion = majorVersion; osvi.dwMinorVersion = minorVersion; osvi.wServicePackMajor = servicePackMajor; return(VerifyVersionInfoW(ref osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, mask)); }
IsWindowsVersionOrGreater(ushort wMajorVersion, ushort wMinorVersion, ushort wServicePackMajor) { var osvi = new OSVERSIONINFOEXW { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEXW)) }; var dwlConditionMask = VerSetConditionMask( VerSetConditionMask( VerSetConditionMask( 0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL), VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); osvi.dwMajorVersion = wMajorVersion; osvi.dwMinorVersion = wMinorVersion; osvi.wServicePackMajor = wServicePackMajor; return(VerifyVersionInfoW(ref osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE); }
static bool IsWindowsVersionOrGreater(ushort wMajorVersion, ushort wMinorVersion, ushort wBuildVersion /* wServicePackMajor */) { OSVERSIONINFOEXW osvi = new OSVERSIONINFOEXW(); osvi.dwOSVersionInfoSize = Marshal.SizeOf(osvi); int buildNumberMask = VER_BUILDNUMBER; /* VER_SERVICEPACKMAJOR */ ulong dwlConditionMask = VerSetConditionMask( VerSetConditionMask( VerSetConditionMask( 0, VER_MAJORVERSION, (byte)VER_GREATER_EQUAL), VER_MINORVERSION, (byte)VER_GREATER_EQUAL), buildNumberMask, (byte)VER_GREATER_EQUAL); osvi.dwMajorVersion = wMajorVersion; osvi.dwMinorVersion = wMinorVersion; osvi.dwBuildNumber = wBuildVersion; // osvi.wServicePackMajor = wServicePackMajor; return(VerifyVersionInfoW(ref osvi, VER_MAJORVERSION | VER_MINORVERSION | buildNumberMask, dwlConditionMask) != false); }
static extern bool VerifyVersionInfoW(ref OSVERSIONINFOEXW lpVersionInformation, uint dwTypeMask, ulong dwlConditionMask);
public static extern bool GetVersionExW(ref OSVERSIONINFOEXW osvi);
extern static int RtlGetVersion(ref OSVERSIONINFOEXW lpVersionInformation);
public static extern bool RtlGetVersion(ref OSVERSIONINFOEXW versionInfo);
private static extern int RtlGetVersion(ref OSVERSIONINFOEXW versionInformation);
/// <summary> /// Checks if the OS supports WAM (Web Account Manager) /// WAM Supported OS's are Windows 10 and above for Client, Windows 2019 and above for Server /// </summary> /// <returns>Returns <c>true</c> if the OS Version has WAM support</returns> public static bool IsWamSupportedOs() { try { #if NET45 var OsVersionInfo = new OSVERSIONINFOEXW { dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEXW)) }; #else var OsVersionInfo = new OSVERSIONINFOEXW { dwOSVersionInfoSize = Marshal.SizeOf <OSVERSIONINFOEXW>() }; #endif if (RtlGetVersion(ref OsVersionInfo) == NT_STATUS_SUCCESS) { switch (OsVersionInfo.wProductType) { //To filter client operating systems only, such as Windows 10 or Windows 11 Clients, use ProductType="1". case VER_NT_WORKSTATION: switch (OsVersionInfo.dwMajorVersion) { //https://docs.microsoft.com/en-us/windows/win32/sysinfo/operating-system-version //For Client (Windows 10 and 11) and for Server (Windows 2016 and above) Major version is 10.* //Windows 10 Build Number 15063 is the minimum version where WAM is supported case 10: if (OsVersionInfo.dwBuildNumber >= WamSupportedWindows10BuildNumber) { return(true); } else { return(false); } default: return(false); } //For server operating systems that are not domain controllers and for Windows 10 and Windows 11 multi-session, use ProductType="3". //https://docs.microsoft.com/en-us/windows/security/threat-protection/windows-firewall/create-wmi-filters-for-the-gpo case VER_NT_SERVER: case VER_NT_DOMAIN_CONTROLLER: switch (OsVersionInfo.dwMajorVersion) { //https://docs.microsoft.com/en-us/windows/win32/sysinfo/operating-system-version //For Client (Windows 10 and 11) and for Server (Windows 2016 and above) Major version is 10.* //Windows Server 2019 minimum build number is 17763 case 10: if (OsVersionInfo.dwBuildNumber >= Windows2019BuildNumber) { return(true); } else { return(false); } default: return(false); } default: return(false); } } return(false); } catch { return(false); } }