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

GetVersionEx deprecation - how to get product type (VER_NT_DOMAIN_CONTROLLER) from newer API's

本文关键字:VER 类型 NT CONTROLLER DOMAIN 获取 弃用 API GetVersionEx      更新时间:2023-10-16

我正在尝试检查操作系统是否是域控制器(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文档