以编程方式获取 Windows 操作系统版本 [英] Getting Windows OS version programmatically

查看:70
本文介绍了以编程方式获取 Windows 操作系统版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Windows 10 机器上使用 C# 获取 Windows 版本.

I am trying to fetch Windows version with C# on my Windows 10 machine.

我总是得到这些值(使用 C#C++):

I always get those values (with C#C++):

专业:6

次要:2

这是 Windows 8 操作系统,根据 MSDN

Which is Windows 8 OS, accordingly to MSDN

C#代码:

var major = OperatingSystem.Version.Major
var minor  = OperatingSystem.Version.Minor

C++ 代码

void print_os_info()
{
    //http://stackoverflow.com/questions/1963992/check-windows-version
    OSVERSIONINFOW info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOW));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);

    LPOSVERSIONINFOW lp_info = &info;
    GetVersionEx(lp_info);

    printf("Windows version: %u.%u
", info.dwMajorVersion, info.dwMinorVersion);
}

Windows 10 假设与那些:

Windows 10 suppose to be with those:

专业:10

次要:0*

  • (当我从正在运行的进程中获取转储文件时,我可以看到该文件的操作系统版本设置为 10.0)
  • 构建者:10.0.10586.0 (th2_release.151029-1700)

    built by: 10.0.10586.0 (th2_release.151029-1700)

    我在这里错过了什么?

    推荐答案

    由于接受的答案仅适用于 C#,因此这里是 C++ 的解决方案.

    As the accepted answer is only for C#, here is a solution for C++.

    它使用 ntdll.dll 中的 RtlGetVersion,它使用与 GetVersionEx 相同的结构(名称不同,但元素相同)并为您提供正确的版本.由于该函数通常用于驱动开发,所以该函数是在DDK中声明的,而不是在SDK中声明的.所以我使用动态解决方案来调用该函数.请注意,每次调用都会加载和释放 ntdll.dll.因此,如果您更频繁地需要该功能,请保持库的加载.

    It uses the RtlGetVersion in the ntdll.dll that uses the same structure as GetVersionEx (name is different, but the elements are the same) and gives you the correct version. As this function is normally used for driver development, the function is declared in the DDK and not in the SDK. So I used a dynamic solution to call the function. Please be aware that the ntdll.dll is loaded and released in every call. So if you need the function more often, keep the library loaded.

    pOSversion 指向的结构必须像 GetVersionEx 一样初始化.

    The structure pOSversion is pointing to must be initialized like for GetVersionEx.

    BOOL GetTrueWindowsVersion(OSVERSIONINFOEX* pOSversion)
    {
       // Function pointer to driver function
       NTSTATUS (WINAPI *pRtlGetVersion)(
          PRTL_OSVERSIONINFOW lpVersionInformation) = NULL;
    
       // load the System-DLL
       HINSTANCE hNTdllDll = LoadLibrary("ntdll.dll");
    
       // successfully loaded?
       if (hNTdllDll != NULL)
       {
          // get the function pointer to RtlGetVersion
          pRtlGetVersion = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW))
                GetProcAddress (hNTdllDll, "RtlGetVersion");
    
          // if successfull then read the function
          if (pRtlGetVersion != NULL)
             pRtlGetVersion((PRTL_OSVERSIONINFOW)pOSversion);
    
          // free the library
          FreeLibrary(hNTdllDll);
       } // if (hNTdllDll != NULL)
    
       // if function failed, use fallback to old version
       if (pRtlGetVersion == NULL)
          GetVersionEx((OSVERSIONINFO*)pOSversion);
    
       // always true ...
       return (TRUE);
    } // GetTrueWindowsVersion
    

    这篇关于以编程方式获取 Windows 操作系统版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆