繁体   English   中英

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 )。 使用OSVERSIONINFOEX GetVersionEx函数很容易做到这一点。 但是GetVersionEx的MSDN页面建议不要使用此功能,并且我们在Visual Studio 2015中看到警告。

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

我查看了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'\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函数| 微软文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM