Convert Environment.OSVersion to NTDDI version format

Posted by David Brown on Stack Overflow See other posts from Stack Overflow or by David Brown
Published on 2010-04-13T02:22:59Z Indexed on 2010/04/13 2:33 UTC
Read the original article Hit count: 746

Filed under:
|
|
|
|

In sdkddkver.h of the Windows Platform SDK, there are various OS versions defined as NTDDI_*.

For example, Windows XP and its service packs are defined as:

#define NTDDI_WINXP     0x05010000
#define NTDDI_WINXPSP1  0x05010100
#define NTDDI_WINXPSP2  0x05010200
#define NTDDI_WINXPSP3  0x05010300
#define NTDDI_WINXPSP4  0x05010400

There are also masks which, along with the OSVER, SPVER, and SUBVER macros, allow you to pull the respective parts out of the NTDDI version.

#define OSVERSION_MASK  0xFFFF0000
#define SPVERSION_MASK  0x0000FF00
#define SUBVERSION_MASK 0x000000FF

I have all of these defined as constants in C# and what I'd like to do now is convert the data returned by Environment.OSVersion to a value corresponding to one of the NTDDI versions in sdkddkver.h. I could make a massive switch statement, but that's not really as future-proof as I'd like it to be. I would need to update the conversion method every time a new OS or service pack is released.

I have a feeling this could be done with the help of some bitwise operators, but I'll be honest and say that those aren't my strong point.

I appreciate any help!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#