少数 Windows 10 个版本的 GetProductInfo 函数 API 中缺少产品类型
Missing product types in GetProductInfo function API for few Windows 10 editions
我正在通过 P-Invoke 使用 GetVersionEx and GetProductInfo APIs 来检测 Windows 10 OS 的版本,代码如下:
P-调用声明:
[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;
}
}
}
}
pdwReturnedProductType
是 GetProductInfo
API 中的输出参数,它根据文档中给出的 table 提供版本值。文档缺少以下版本的十六进制值:
- Windows 10 专业教育
- Windows 10 企业 2019 LTSC
我需要这些值来覆盖我代码中的所有版本。不确定为什么官方 MS 文档中缺少这些代码。
所有内容都记录在 winnt.h
的 Windows SDK (C:\Program Files (x86)\Windows Kits\Include.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。
我正在通过 P-Invoke 使用 GetVersionEx and GetProductInfo APIs 来检测 Windows 10 OS 的版本,代码如下:
P-调用声明:
[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;
}
}
}
}
pdwReturnedProductType
是 GetProductInfo
API 中的输出参数,它根据文档中给出的 table 提供版本值。文档缺少以下版本的十六进制值:
- Windows 10 专业教育
- Windows 10 企业 2019 LTSC
我需要这些值来覆盖我代码中的所有版本。不确定为什么官方 MS 文档中缺少这些代码。
所有内容都记录在 winnt.h
的 Windows SDK (C:\Program Files (x86)\Windows Kits\Include.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。