Convert Environment.OSVersion to NTDDI version format
- by David Brown
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!