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

标签 c++ winapi

我正在尝试检查操作系统是否是域 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/

相关文章:

c++ - 如何获取显示设备的驱动版本?

python - CreateProcess 如何定位可执行文件?

winapi - WaitForSingleObject超时解决

c++ - QtConcurrent::map 显示没有任何好处

c++ - 如何重置for循环中的值?

c++ - 我如何证明 delete 不会释放 new [] 分配的所有内存?

不为右值引用调用 C++ move 构造函数

c++ - 按下了什么键?键盘 Hook

c++ - MT4 DLL/TA-LIB 链接器错误

c++ - ReadProcessMemory 的缓冲区应该是什么数据类型?