GetVersionEx 弃用 - 如何从较新的 API 获取产品类型 (VER_NT_DOMAIN_CONTROLLER)
GetVersionEx deprecation - how to get product type (VER_NT_DOMAIN_CONTROLLER) from newer API's
我正在尝试检查操作系统是否是域控制器(VER_NT_DOMAIN_CONTROLLER
(。使用GetVersionEx
功能很容易做到这一点 OSVERSIONINFOEX
.但是GetVersionEx
的 MSDN 页面表明此函数已弃用,我们在 Visual Studio 2015 中也看到了警告。
是否有任何更新的 API 可以提供此信息?我知道有更新的版本助手函数可以告诉它是哪种操作系统,但我没有看到任何获取产品类型的内容。
我查看了 NodeJS/libuv 如何解决操作系统版本号,以便自己弄清楚如何做到这一点。如果可用,他们使用RtlGetVersion()
,否则他们会回退到GetVersionEx()
。
我想出的解决方案是:
// Windows10SCheck.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <Windows.h>
// Function to get the OS version number
//
// Uses RtlGetVersion() is available, otherwise falls back to GetVersionEx()
bool getosversion(OSVERSIONINFOEX* osversion) {
NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEX);
*(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");
if (RtlGetVersion != NULL)
{
// RtlGetVersion uses 0 (STATUS_SUCCESS)
// as return value when succeeding
return RtlGetVersion(osversion) == 0;
}
else {
// GetVersionEx was deprecated in Windows 10
// Only use it as fallback
#pragma warning(suppress : 4996)
return GetVersionEx((LPOSVERSIONINFO)osversion);
}
}
int main()
{
OSVERSIONINFOEX osinfo;
osinfo.dwOSVersionInfoSize = sizeof(osinfo);
osinfo.szCSDVersion[0] = L' ';
if (!getosversion(&osinfo)) {
std::cout << "Failed to get OS versionn";
}
std::cout << osinfo.dwMajorVersion << "." << osinfo.dwMinorVersion << "." << osinfo.dwBuildNumber << "n";
DWORD dwReturnedProductType = 0;
if (!GetProductInfo(osinfo.dwMajorVersion, osinfo.dwMinorVersion, 0, 0, &dwReturnedProductType)) {
std::cout << "Failed to get product infon";
}
std::cout << "Product type: " << std::hex << dwReturnedProductType;
}
我的机器上的输出:
10.0.15063
Product type: 1b
产品类型的含义可以在以下位置找到:获取产品信息功能 |Microsoft文档
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型