c# - 少数 Windows 10 版本的 GetProductInfo 函数 API 中缺少产品类型
问题描述
我通过 P-Invoke 使用GetVersionEx和GetProductInfo 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;
}
}
}
}
pdwReturnedProductType
是GetProductInfo
API 中的 out 参数,它根据文档中给出的表格提供 edition 的值。该文档缺少以下版本的十六进制值:
- Windows 10 专业教育版
- Windows 10 企业版 2019 LTSC
我需要这些值来涵盖我的代码中的所有版本。不知道为什么官方 MS 文档中缺少这些代码。
解决方案
一切都记录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。
推荐阅读
- php - CURL HTTP 标头编码
- javascript - javascript中的数字格式输入字段问题
- windows - 在 Windows 上查找 docker 镜像文件
- php - MongoDB & PHP - 处理重复文档
- python - 如何在 Tkinter 中设置 SpinBox 的默认值?
- c# - 如何获取 ReportLevelFilters?
- javascript - 两个字符串在 while 循环中不相等 - subString 问题?
- docker - 使用 Atom 通过 FTP 访问 Mayan EDMS 文件系统时出现问题
- kubernetes - 将命名空间解析为值文件
- css - R Shiny tooltip css没有被应用