10

I have scenarios where i want to specifically know the OS major/minor version and build number etc.

From windows 8.1 onwards GetVersion and GetVersionEx have been deprecated, stating:

[GetVersion/GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead, use the Version Helper functions]

None of the version helper APIs help me get the OS version number rather help me verify or get to know if my version is same or above some mentioned version. What can be done?

valiano
  • 16,433
  • 7
  • 64
  • 79
Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
  • 1
    I've never yet known Microsoft to remove an API function even when it is deprecated, so I'd say keep using it if you want. Just be aware that in later versions of Windows it lies about the version number unless your manifest specifies compatibility with the current version. – Jonathan Potter Dec 02 '14 at 10:34
  • 2
    There is WMI's Win32_OperatingSystem class – Alex K. Dec 02 '14 at 12:11
  • What @Alex says is good. You can always read the version resource from kernel32. – David Heffernan Dec 03 '14 at 07:28
  • What is your scenario here exactly? Also, see [this](http://stackoverflow.com/questions/22303824/warning-c4996-getversionexw-was-declared-deprecated) stack overflow thread. – Chuck Walbourn Dec 05 '14 at 21:01
  • @AlexK. I think you should add it as an answer to this question, because none of the answers were speaking about the WMI query – Ram Dec 06 '17 at 09:25

3 Answers3

6

The API GetVersionEx() continues to work in Windows 8.1+, but Microsoft has altered its functionality. From MSDN (emphasis mine):

With the release of Windows 8.1, the behavior of the GetVersionEx API has changed in the value it will return for the operating system version. The value returned by the GetVersionEx function now depends on how the application is manifested.

Applications not manifested for Windows 8.1 will return the Windows 8 OS version value (6.2). Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. To manifest your applications for Windows 8.1 please refer to Targeting your application for Windows 8.1.

What you need to do is add the proper GUID(s) to your application (.exe/.dll) binaries (via manifest XML information). In other words, if you specifically state your application supports 8.1, GetVersionEx() will return proper information when running on Windows 8.1. If you do not, GetVersionEx() will lie to you.

See Targeting your application For Windows 8.1 for a list of GUIDs. Also covered here and here.

GUID List for the Lazy

  • Vista / Server 2008: {e2011457-1546-43c5-a5fe-008deee3d3f0}
  • Windows 7 / Server 2008 R2: {35138b9a-5d96-4fbd-8e2d-a2440225f93a}
  • Windows 8 / Server 2012: {4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}
  • Windows 8.1 / Server 2012 R2 : {1f676c76-80e1-4239-95bb-83d0f6d0da78}
  • Windows 10 / Server 2016: {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}

As for Windows Server 2019, I'm not sure that a new GUID has been released. Please comment if you know more!

Ken White
  • 123,280
  • 14
  • 225
  • 444
NuSkooler
  • 5,391
  • 1
  • 34
  • 58
  • What if my application does not have a mainfest? – Abhishek Jain Dec 03 '14 at 04:57
  • You can simply add one in your projects settings. See for example here: http://msdn.microsoft.com/en-us/library/ms235229%28v=vs.110%29.aspx – NuSkooler Dec 03 '14 at 17:22
  • 8
    Keep in mind that unless you constantly update your application, eventually your manifest will not include the 'latest' version of Windows and you'll get a version lie. – Chuck Walbourn Dec 05 '14 at 21:00
  • 1
    Also keep in mind that unless you update and test your application on the latest versions it can very likely break. Case in point: GetVersionEx() stops functioning as expected. This is WAD. – NuSkooler Dec 09 '14 at 17:24
  • In order to target Windows 8.1, you need to either include the app manifest or include _NT_TARGET_VERSION=$ (_NT_TARGET_VERSION_LATEST) in the source file. Question is where to put include _NT_TARGET_VERSION? which source file? – Abhishek Jain Dec 19 '14 at 04:46
0

There is a new function named GetProductInfo that returns version infos.

If you want to test for a specific version you should use even VerifyVersionInfo

It is easy to create a structure to check whether a specific OS version is running. VerifyVersionInfo takes to version structures and you can easily check for VER_GREATER_EQUAL and VER_LESS_EQUAL

Also note that GetVersionEx doesn't lie on a Windows 8.1 system if you define the correct supported OS entry in the compatibility section for your manifest. But it may lie in a future OS version!

See Targeting your application For Windows 8.1 for a list of GUIDs. Also covered here.

GUID list for the application manifest

  • Vista: {e2011457-1546-43c5-a5fe-008deee3d3f0}
  • Windows 7: {35138b9a-5d96-4fbd-8e2d-a2440225f93a}
  • Windows 8: {4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}
  • Windows 8.1: {1f676c76-80e1-4239-95bb-83d0f6d0da78}
  • Windows 10: {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • I want to get the major, minor and build number of the operating system i am running my program? How can get that using GetProductInfo and VerifyVersionInfo? – Abhishek Jain Dec 02 '14 at 12:59
  • You can get the service pack major number with VerifyVersionInfo. I don't know any situation where a OS build number inside a SP differs... – xMRi Dec 02 '14 at 14:02
  • 10
    `GetProductInfo()` does not return the OS version. You pass it in a desired OS version, and it returns the *product type* of the OS (business, enterprise, datacenter, etc). This is clearly stated in the [documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724358.aspx): "Retrieves the product type for the operating system on the local computer, and maps the type to the product types supported by the specified operating system." – Remy Lebeau Dec 02 '14 at 17:40
-11

Check this article on codeproject.com, It's working perfectly for Windows 8 :

1) Download .DLL and add it to your project .

2) Use this code to get Operation System Information :

StringBuilder sb = new StringBuilder(String.Empty);
sb.AppendLine("Operation System Information");
sb.AppendLine("----------------------------");
sb.AppendLine(String.Format("Name = {0}", OSVersionInfo.Name));
sb.AppendLine(String.Format("Edition = {0}", OSVersionInfo.Edition));
if (OSVersionInfo.ServicePack!=string.Empty)
sb.AppendLine(String.Format("Service Pack = {0}", OSVersionInfo.ServicePack));
else
sb.AppendLine("Service Pack = None");
sb.AppendLine(String.Format("Version = {0}", OSVersionInfo.VersionString));
sb.AppendLine(String.Format("ProcessorBits = {0}", OSVersionInfo.ProcessorBits));
sb.AppendLine(String.Format("OSBits = {0}", OSVersionInfo.OSBits));
sb.AppendLine(String.Format("ProgramBits = {0}", OSVersionInfo.ProgramBits));

textBox1.Text = sb.ToString();