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

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

问题描述

我正在尝试在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

Major: 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\n", info.dwMajorVersion, info.dwMinorVersion);
}

Windows 10应该与以下版本一起使用:

Windows 10 suppose to be with those:

专业:10

Major: 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 OS版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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