How Do You Detect if Running OS is Windows 7 or Later?


Recently I need to write some codes to disable one feature on Windows 7 for an application. For Windows 7, its major number is 6 and minor number is 1 (see this link http://msdn.microsoft.com/en-us/library/ms724832(VS.85).aspx) and thus we can compare the version number to detect that. However, that is not enough. Because Windows Server 2008 R2 also use this version number, we have to filter it further by using product type. In our managed code, System.OperatingSystem can retrieve version number easily, but there is no available API for retrieving product type, so we have to wrap native function GetVersionEx(). Something like this: 


            if (osInfo.Platform == System.PlatformID.Win32NT && osInfo.Version.Major == 6 && osInfo.Version.Minor == 1)


            {


                if (SafeNativeMethods.GetVersionEx(versionInfo))


                {


                    if (versionInfo.wProductType == ProductType.Windows_Workstation)


                    {


                        // This is Windows 7


                    }


                }


            }


 


       [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]


       [return: MarshalAs(UnmanagedType.Bool)]


       internal extern static bool GetVersionEx([In, Out] OSVERSIONINFO pVersionInfo);


However, it is NOT GOOD! Sorry for that.......Let us read this MSDN web page:


http://msdn.microsoft.com/en-us/library/ms724832.aspx


It points out at least two points:



  1. If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows.

  2. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself.

Obviously, GetVersionEx() is not suitable here. VerifyVersionInfo() should be the friend. The following link gives very good example in C++ to show how to detect if the OS is Windows XP SP2 or later.


http://msdn.microsoft.com/en-us/library/ms725491(VS.85).aspx


Following the example in above link, we can wrap VerifyVersionInfo() in managed code to detect if the running OS is Windows 7 or later.

Comments (0)

Skip to main content