如果您在 cmd 中输入 ver
,您会得到如下内容:
Microsoft Windows [Version 10.0.17192.162]
我是否可以访问此信息以在我的 C 程序中使用?我需要找到一个人正在运行的 Windows 版本。我已经检查过 SYSTEM_INFO:
typedef struct _SYSTEM_INFO {
union {
DWORD dwOemId;
struct {
WORD wProcessorArchitecture;
WORD wReserved;
};
};
DWORD dwPageSize;
LPVOID lpMinimumApplicationAddress;
LPVOID lpMaximumApplicationAddress;
DWORD_PTR dwActiveProcessorMask;
DWORD dwNumberOfProcessors;
DWORD dwProcessorType;
DWORD dwAllocationGranularity;
WORD wProcessorLevel;
WORD wProcessorRevision;
} SYSTEM_INFO;
和操作系统版本信息
typedef struct _OSVERSIONINFOA {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
CHAR szCSDVersion[128];
} OSVERSIONINFOA, *POSVERSIONINFOA, *LPOSVERSIONINFOA;
但都不包含完整的版本信息。
此外,为了检索操作系统的名称,除了进行 #ifdef __WIN32
检查之外,还有其他方法吗?
最佳答案
如果想得到与cmd相同的结果可以运行下面的代码:
ULONG GetVersionRevision(PULONG Ubr)
{
HKEY hKey;
ULONG dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ|KEY_WOW64_64KEY, &hKey);
if (dwError == NOERROR)
{
ULONG Type;
ULONG cb = sizeof(ULONG);
dwError = RegQueryValueExW(hKey, L"UBR", 0, &Type, (PBYTE)Ubr, &cb);
if (dwError == NOERROR)
{
if (Type != REG_DWORD || cb != sizeof(ULONG))
{
dwError = ERROR_GEN_FAILURE;
}
}
RegCloseKey(hKey);
}
return dwError;
}
ULONG M, m, b, Ubr;
RtlGetNtVersionNumbers(&M, &m, &b);
if (GetVersionRevision(&Ubr) == NOERROR)
{
DbgPrint("[Version %u.%u.%u.%u]\n", M, m, b & 0xffff, Ubr);
}
else
{
DbgPrint("[Version %u.%u.%u]\n", M, m, b & 0xffff);
}
关于c - 获取操作系统的名称和版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807990/