首页 > 解决方案 > 少数 Windows 10 版本的 GetProductInfo 函数 API 中缺少产品类型

问题描述

我通过 P-Invoke 使用GetVersionExGetProductInfo API 来根据以下代码检测 Windows 10 操作系统的版本:

P-Invoke 声明:

[DllImport("Kernel32.dll")]
internal static extern bool GetProductInfo(
int osMajorVersion,
int osMinorVersion,
int spMajorVersion,
int spMinorVersion,
out int edition);

[DllImport("kernel32.dll")]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);

[StructLayout(LayoutKind.Sequential)]
private struct OSVERSIONINFOEX
{
    public int dwOSVersionInfoSize;
    public int dwMajorVersion;
    public int dwMinorVersion;
    public int dwBuildNumber;
    public int dwPlatformId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string szCSDVersion;
    public short wServicePackMajor;
    public short wServicePackMinor;
    public short wSuiteMask;
    public byte wProductType;
    public byte wReserved;
}

代码:

var edition = "";

var osVersionInfo = new OSVERSIONINFOEX
{
    dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX))
};

if (GetVersionEx(ref osVersionInfo))
{
    if (osVersionInfo.dwMajorVersion == 10
        && osVersionInfo.wProductType == 1)
    {
        int ed;
        if (GetProductInfo(Environment.OSVersion.Version.Major
                            , Environment.OSVersion.Version.Minor
                            , osVersionInfo.wServicePackMajor
                            , osVersionInfo.wServicePackMinor
                            , out ed))
        {
            switch (ed)
            {
                case PRODUCT_PROFESSIONAL:
                    edition = "Windows 10 Pro";
                    break;
                case PRODUCT_PRO_WORKSTATION:
                    edition = "Windows 10 Pro for Workstations";
                    break;
                case PRODUCT_EDUCATION:
                    edition = "Windows 10 Education";
                    break;
                case PRODUCT_ENTERPRISE:
                    edition = "Windows 10 Enterprise";
                    break;
                //case 0x1234:
                //    edition = "Windows 10 Enterprise 2019 LTSC";
                //    break;
                //case 0x2345:
                //    edition = "Windows 10 Pro Education";
                //    break;
                default:
                    edition = "Unknown";
                    break;
            }

        }
    }
}

pdwReturnedProductTypeGetProductInfoAPI 中的 out 参数,它根据文档中给出的表格提供 edition 的值。该文档缺少以下版本的十六进制值:

  1. Windows 10 专业教育版
  2. Windows 10 企业版 2019 LTSC

我需要这些值来涵盖我的代码中的所有版本。不知道为什么官方 MS 文档中缺少这些代码。

标签: c#windowswinapiwindows-10pinvoke

解决方案


一切都记录winnt.h在 Windows SDK 中(C:\Program Files (x86)\Windows Kits\10\Include\10.0.BUILDNUMBER.0\um)

#define PRODUCT_PRO_FOR_EDUCATION                   0x000000A4
#define PRODUCT_ENTERPRISE_S                        0x0000007D

此 PRODUCT_ENTERPRISE_S 是 LTSB/LTSC 版本。

要检查您使用的 LTSB/LTSC 版本,还可以ReleaseID从注册表中读取。1809 是 2019 LTSC,1607 是 2016 长期版本 LTSB。


推荐阅读