2 using System.Runtime.InteropServices;
3 using System.Runtime.InteropServices.ComTypes;
5 namespace NaGet.InteropServices
10 public sealed class WindowsVersion
12 [DllImport("ntdll.dll", SetLastError=true)]
13 private static extern int RtlGetVersion([In, Out] ref OSVERSIONINFOEX lpVersionInformation);
15 [StructLayout(LayoutKind.Sequential)]
16 private struct OSVERSIONINFOEX
18 public uint dwOSVersionInfoSize;
19 public uint dwMajorVersion;
20 public uint dwMinorVersion;
21 public uint dwBuildNumber;
22 public uint dwPlatformId;
23 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
24 public string szCSDVersion;
25 public ushort wServicePackMajor;
26 public ushort wServicePackMinor;
27 public ushort wSuiteMask;
28 public char wProductType;
29 public char wReserved;
32 public struct VersionInfo {
33 public uint MajorVersion;
34 public uint MinorVersion;
35 public uint BuildNumber;
36 public ushort ServicePackMajor;
37 public ushort ServicePackMinor;
40 public static VersionInfo? GetVersionInfo() {
41 VersionInfo? retval = null;
44 OSVERSIONINFOEX versionInformation = new OSVERSIONINFOEX();
45 versionInformation.dwOSVersionInfoSize = (uint)Marshal.SizeOf(versionInformation);
47 int result = RtlGetVersion(ref versionInformation);
49 VersionInfo info = new VersionInfo();
50 info.MajorVersion = versionInformation.dwMajorVersion;
51 info.MinorVersion = versionInformation.dwMinorVersion;
52 info.BuildNumber = versionInformation.dwBuildNumber;
53 info.ServicePackMajor = versionInformation.wServicePackMajor;
54 info.ServicePackMinor = versionInformation.wServicePackMinor;