gpt4 book ai didi

c++ - GetVersionEx 弃用 - 如何从较新的 API 获取产品类型 (VER_NT_DOMAIN_CONTROLLER)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:46:10 26 4
gpt4 key购买 nike

我正在尝试检查操作系统是否是域 Controller (VER_NT_DOMAIN_CONTROLLER)。使用使用 OSVERSIONINFOEXGetVersionEx 函数很容易做到这一点.但是 GetVersionEx 的 MSDN 页面表明此函数已弃用,而且我们在 visual studio 2015 中看到了警告。

是否有更新的 API 可以提供此信息?我知道有更新的 Version Helper functions告诉它是什么类型的操作系统,但我没有看到任何获取产品类型的信息。

最佳答案

我查看了 NodeJS/libuv solved the OS version number为了弄清楚如何自己做。他们使用 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'\0';

if (!getosversion(&osinfo)) {
std::cout << "Failed to get OS version\n";
}

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 info\n";
}

std::cout << "Product type: " << std::hex << dwReturnedProductType;

}

我机器上的输出:

10.0.15063
Product type: 1b

可以在以下位置找到产品类型的含义:GetProductInfo function | Microsoft Docs

关于c++ - GetVersionEx 弃用 - 如何从较新的 API 获取产品类型 (VER_NT_DOMAIN_CONTROLLER),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com