04 February, 2009

Получение полного имени OS

Зачем я публикую эту информацию? Все просто, когда она мне понадобилась я потратил кучу времени, чтобы ее найти в достаточно полном объеме.

Сразу оговорюсь, что OS семейства Windows 95/98/Milenium/NT 3.5x/NT 4.0 я не рассматриваю - так как у меня есть причины считать, что эти OS уже не встречаются сегодня достаточно широко.

Теперь несколько комментариев к аргументам функции. processorArchitecture получается при помощи native вызова GetNativeSystemInfo(). GetVersionEx() дает нам majorVersion, minorVersion, buildNumber, csdVersion, suiteMask и productType. GetProductInfo() - vistaProductType. А флаги IsXPTabletPC, IsXPMediaCenter, IsXPStarter, Is2K3ServerR2 - при помощи нескольких вызовов GetSystemMetrics() с соответствующими аргументами.

public enum ProcessorArchitecture : ushort
{
  PROCESSOR_ARCHITECTURE_INTEL = 0,
  PROCESSOR_ARCHITECTURE_MIPS = 1,
  PROCESSOR_ARCHITECTURE_ALPHA = 2,
  PROCESSOR_ARCHITECTURE_PPC = 3,
  PROCESSOR_ARCHITECTURE_SHX = 4,
  PROCESSOR_ARCHITECTURE_ARM = 5,
  PROCESSOR_ARCHITECTURE_IA64 = 6,
  PROCESSOR_ARCHITECTURE_ALPHA64 = 7,
  PROCESSOR_ARCHITECTURE_MSIL = 8,
  PROCESSOR_ARCHITECTURE_AMD64 = 9,
  PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 = 10,
}

[Flags]
public enum SuiteMask : ushort
{
  VER_SUITE_SMALLBUSINESS = 0x0001,
  VER_SUITE_ENTERPRISE = 0x0002,
  VER_SUITE_BACKOFFICE = 0x0004,
  VER_SUITE_COMMUNICATIONS = 0x0008,
  VER_SUITE_TERMINAL = 0x0010,
  VER_SUITE_SMALLBUSINESS_RESTRICTED = 0x0020,
  VER_SUITE_EMBEDDEDNT = 0x0040,
  VER_SUITE_DATACENTER = 0x0080,
  VER_SUITE_SINGLEUSERTS = 0x0100,
  VER_SUITE_PERSONAL = 0x0200,
  VER_SUITE_BLADE = 0x0400,
  VER_SUITE_EMBEDDED_RESTRICTED = 0x0800,
  VER_SUITE_SECURITY_APPLIANCE = 0x1000,
  VER_SUITE_STORAGE_SERVER = 0x2000,
  VER_SUITE_COMPUTE_SERVER = 0x4000,
  VER_SUITE_WH_SERVER = 0x8000,
}

public enum ProductType : ushort
{
  VER_NT_WORKSTATION = 0x0001,
  VER_NT_DOMAIN_CONTROLLER = 0x0002,
  VER_NT_SERVER = 0x0003,
}

public enum Product : uint
{
  PRODUCT_UNDEFINED = 0x00000000,

  PRODUCT_ULTIMATE = 0x00000001,
  PRODUCT_HOME_BASIC = 0x00000002,
  PRODUCT_HOME_PREMIUM = 0x00000003,
  PRODUCT_ENTERPRISE = 0x00000004,
  PRODUCT_HOME_BASIC_N = 0x00000005,
  PRODUCT_BUSINESS = 0x00000006,
  PRODUCT_STANDARD_SERVER = 0x00000007,
  PRODUCT_DATACENTER_SERVER = 0x00000008,
  PRODUCT_SMALLBUSINESS_SERVER = 0x00000009,
  PRODUCT_ENTERPRISE_SERVER = 0x0000000A,
  PRODUCT_STARTER = 0x0000000B,
  PRODUCT_DATACENTER_SERVER_CORE = 0x0000000C,
  PRODUCT_STANDARD_SERVER_CORE = 0x0000000D,
  PRODUCT_ENTERPRISE_SERVER_CORE = 0x0000000E,
  PRODUCT_ENTERPRISE_SERVER_IA64 = 0x0000000F,
  PRODUCT_BUSINESS_N = 0x00000010,
  PRODUCT_WEB_SERVER = 0x00000011,
  PRODUCT_CLUSTER_SERVER = 0x00000012,
  PRODUCT_HOME_SERVER = 0x00000013,
  PRODUCT_STORAGE_EXPRESS_SERVER = 0x00000014,
  PRODUCT_STORAGE_STANDARD_SERVER = 0x00000015,
  PRODUCT_STORAGE_WORKGROUP_SERVER = 0x00000016,
  PRODUCT_STORAGE_ENTERPRISE_SERVER = 0x00000017,
  PRODUCT_SERVER_FOR_SMALLBUSINESS = 0x00000018,
  PRODUCT_SMALLBUSINESS_SERVER_PREMIUM = 0x00000019,
  PRODUCT_HOME_PREMIUM_N = 0x0000001A,
  PRODUCT_ENTERPRISE_N = 0x0000001B,
  PRODUCT_ULTIMATE_N = 0x0000001C,
  PRODUCT_WEB_SERVER_CORE = 0x0000001D,
  PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT = 0x0000001E,
  PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY = 0x0000001F,
  PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING = 0x00000020,
  PRODUCT_SMALLBUSINESS_SERVER_PRIME = 0x00000021,
  PRODUCT_HOME_PREMIUM_SERVER = 0x00000022,
  PRODUCT_SERVER_FOR_SMALLBUSINESS_V = 0x00000023,
  PRODUCT_STANDARD_SERVER_V = 0x00000024,
  PRODUCT_DATACENTER_SERVER_V = 0x00000025,
  PRODUCT_ENTERPRISE_SERVER_V = 0x00000026,
  PRODUCT_DATACENTER_SERVER_CORE_V = 0x00000027,
  PRODUCT_STANDARD_SERVER_CORE_V = 0x00000028,
  PRODUCT_ENTERPRISE_SERVER_CORE_V = 0x00000029,
  PRODUCT_HYPERV = 0x0000002A,
}

static string GetOSName_Win32NT(
  ProcessorArchitecture nativeArchitecture,
  int majorVersion,
  int minorVersion,
  int buildNumber,
  string csdVersion,
  SuiteMask suiteMask,
  ProductType productType,
  Product vistaProductType,
  bool IsXPTabletPC,
  bool IsXPMediaCenter,
  bool IsXPStarter,
  bool Is2K3ServerR2)
{
  var builder = new StringBuilder();
  if (majorVersion == 6 && minorVersion == 0)
  {
    builder.Append(productType == ProductType.VER_NT_WORKSTATION
               ? "Windows Vista"
               : "Windows Server 2008");
    switch (vistaProductType)
    {
    case Product.PRODUCT_ULTIMATE:
      builder.Append(" Ultimate Edition");
      break;
    case Product.PRODUCT_HOME_PREMIUM:
      builder.Append(" Home Premium Edition");
      break;
    case Product.PRODUCT_HOME_BASIC:
      builder.Append(" Home Basic Edition");
      break;
    case Product.PRODUCT_ENTERPRISE:
      builder.Append(" Enterprise Edition");
      break;
    case Product.PRODUCT_BUSINESS:
      builder.Append(" Business Edition");
      break;
    case Product.PRODUCT_STARTER:
      builder.Append(" Starter Edition");
      break;
    case Product.PRODUCT_CLUSTER_SERVER:
      builder.Append(" Cluster Server Edition");
      break;
    case Product.PRODUCT_DATACENTER_SERVER:
      builder.Append(" Datacenter Edition");
      break;
    case Product.PRODUCT_DATACENTER_SERVER_CORE:
      builder.Append(" Datacenter Edition (core installation)");
      break;
    case Product.PRODUCT_ENTERPRISE_SERVER:
      builder.Append(" Enterprise Edition");
      break;
    case Product.PRODUCT_ENTERPRISE_SERVER_CORE:
      builder.Append(" Enterprise Edition (core installation)");
      break;
    case Product.PRODUCT_ENTERPRISE_SERVER_IA64:
      builder.Append(" Enterprise Edition for Itanium-based Systems");
      break;
    case Product.PRODUCT_SMALLBUSINESS_SERVER:
      builder.Append(" Small Business Server");
      break;
    case Product.PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
      builder.Append(" Small Business Server Premium Edition");
      break;
    case Product.PRODUCT_STANDARD_SERVER:
      builder.Append(" Standard Edition");
      break;
    case Product.PRODUCT_STANDARD_SERVER_CORE:
      builder.Append(" Standard Edition (core installation)");
      break;
    case Product.PRODUCT_WEB_SERVER:
      builder.Append(" Web Server Edition");
      break;
    }
    switch (nativeArchitecture)
    {
    case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64:
      builder.Append(" 64-bit");
      break;
    case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL:
      builder.Append(" 32-bit");
      break;
    }
  }
  else if (majorVersion == 5 && minorVersion == 2)
  {
    if (Is2K3ServerR2)
      builder.Append((suiteMask & SuiteMask.VER_SUITE_STORAGE_SERVER) != 0
                 ? "Windows Storage Server 2003 R2"
                 : "Windows Server 2003 R2");
    else if (productType == ProductType.VER_NT_WORKSTATION &&
         nativeArchitecture == ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64)
      builder.Append("Microsoft Windows XP Professional x64 Edition");
    else
      builder.Append((suiteMask & SuiteMask.VER_SUITE_STORAGE_SERVER) != 0
                 ? "Windows Storage Server 2003"
                 : "Windows Server 2003");

    if (productType != ProductType.VER_NT_WORKSTATION)
      if (nativeArchitecture == ProcessorArchitecture.PROCESSOR_ARCHITECTURE_IA64)
        if ((suiteMask & SuiteMask.VER_SUITE_DATACENTER) != 0)
          builder.Append(" Datacenter Edition for Itanium-based Systems");
        else if ((suiteMask & SuiteMask.VER_SUITE_ENTERPRISE) != 0)
          builder.Append(" Enterprise Edition for Itanium-based Systems");
        else
          builder.Append(" Standard Edition for Itanium-based Systems");
      else if (nativeArchitecture == ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64)
        if ((suiteMask & SuiteMask.VER_SUITE_DATACENTER) != 0)
          builder.Append(" Datacenter x64 Edition");
        else if ((suiteMask & SuiteMask.VER_SUITE_ENTERPRISE) != 0)
          builder.Append(" Enterprise x64 Edition");
        else
          builder.Append(" Standard x64 Edition");
      else if ((suiteMask & SuiteMask.VER_SUITE_COMPUTE_SERVER) != 0)
        builder.Append(" Compute Cluster Edition");
      else if ((suiteMask & SuiteMask.VER_SUITE_DATACENTER) != 0)
        builder.Append(" Datacenter Edition");
      else if ((suiteMask & SuiteMask.VER_SUITE_ENTERPRISE) != 0)
        builder.Append(" Enterprise Edition");
      else if ((suiteMask & SuiteMask.VER_SUITE_BLADE) != 0)
        builder.Append(" Web Edition");
      else
        builder.Append(" Standard Edition");
  }
  else if (majorVersion == 5 && minorVersion == 1)
  {
    builder.Append("Microsoft Windows XP");
    if ((suiteMask & SuiteMask.VER_SUITE_PERSONAL) != 0)
      builder.Append(" Home Edition");
    else if ((suiteMask & SuiteMask.VER_SUITE_EMBEDDEDNT) != 0)
      builder.Append(" Embedded Edition");
    else if (IsXPMediaCenter)
      builder.Append(" Media Center Edition");
    else if (IsXPStarter)
      builder.Append(" Starter Edition");
    else if (IsXPTabletPC)
      builder.Append(" Tablet PC Edition Edition");
    else
      builder.Append(" Professional");
  }
  else if (majorVersion == 5 && minorVersion == 0)
  {
    builder.Append("Microsoft Windows 2000");
    if (productType == ProductType.VER_NT_WORKSTATION)
      builder.Append(" Professional");
    else if ((suiteMask & SuiteMask.VER_SUITE_DATACENTER) != 0)
      builder.Append(" Datacenter Server");
    else if ((suiteMask & SuiteMask.VER_SUITE_ENTERPRISE) != 0)
      builder.Append(" Advanced Server");
    else
      builder.Append(" Server");
  }

  if (builder.Length > 0)
  {
    if (csdVersion.Length > 0)
    {
      builder.Append(' ');
      builder.Append(csdVersion);
    }

    builder.Append(" (Build ");
    builder.Append(buildNumber);
    builder.Append(")");

    return builder.ToString();
  }
  return null;
}

No comments: