Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetVersionEx() is deprecated: should I use Environment.OSVersion?

Tags:

c#

.net

winapi

I am intending to write code to determine if the OS is Windows XP, so I can set the LOCALAPPDATA environment variable to work-around a whole load of code which uses ExpandEnvironmentVariables() heavily.

To make it even more fun, some code is written in VB6, and some code is written in C# 4.0 . Looking at the documentation for GetVersionEx(), there are strong suggestions that this API call will be deprecated from Windows 8.1 onwards. But no problem, there is a different set of API calls (VerifyVersionInfo / VerSetConditionMask) I can use. With VB6, there is no choice - I have to use the API call.

However, with my C# code, there appears to be no obvious equivalent. There is Environment.OSVersion, but this seems to be a hacked together set of data from disparate sources, and there isn't anything with the subtlety of the VerifyVersionInfo() API.

Is there a wrapper for this API call. If not, should I bother implementating it myself?

[Added] Or otherwise, maybe someone might have some internal information about how the Environment.OSVersion is implemented?

like image 265
Mark Bertenshaw Avatar asked Sep 20 '13 14:09

Mark Bertenshaw


2 Answers

It's perfectly fine to use GetVersionEx. It won't be removed from Windows for a very long time, if ever. Microsoft has a long track record of maintaining compatibility with old programs. For example, you can still call the Win16 APIs that were deprecated 20 years ago.

like image 45
David Heffernan Avatar answered Oct 09 '22 18:10

David Heffernan


First of all it is important to understand what Deprecated means. It means that the function either is critically broken (such as some early threading), or that it's been superseded (replaced) with new functionality. In this case it is the latter.

From the documentation on GetVersionEx function there is a link suggesting to use Version Helper APIs instead.

One part of this page is specifically important:

Note These APIs are defined by versionhelper.h, which is included in the Windows 8.1 Preview software development kit (SDK). This file can be used with other Microsoft Visual Studio releases to implement the same functionality for Windows versions prior to Windows 8.1 Preview.

like image 200
Seph Avatar answered Oct 09 '22 17:10

Seph