从 Win32 Api C++ 获取操作系统构建版本 [英] Getting OS build version from Win32 Api C++

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

问题描述

我正在尝试查找 Windows Server 2016 机器的构建版本,例如 RS1 或 RS3.有一个 API 可以执行此操作 - GetVersionEx() - 但现在已弃用.

I am trying to find the build version of Windows Server 2016 machines, for example RS1 or RS3. There was an API to do this - GetVersionEx() - but is now deprecated.

MSDN 说要使用 版本辅助函数相反.

MSDN says to use Version Helper Functions instead.

我想要构建版本,例如:1607 for RS1.

I want the build version, for ex: 1607 for RS1.

是否有 API 可以获取此信息?

Is there an API to get this?

推荐答案

Option 0: (per RbMm) 使用驱动程序开发工具包中的 [RtlGetVersion].

Option 0: (per RbMm) Use [RtlGetVersion] from the driver development kit.

选项 1:[更新] 获取像 kernel32.dll 这样的系统 DLL 的版本号.MSDN曾经祝福这种方法,说:

Option 1: [Updated] Grab the version number of a system DLL like kernel32.dll. MSDN used to bless this approach, saying:

要获取操作系统的完整版本号,请对系统 DLL 之一(例如 Kernel32.dll)调用 GetFileVersionInfo 函数,然后调用 VerQueryValue 获取文件版本信息的 \StringFileInfo\\ProductVersion 子块.[来自 2017 年左右 MSDN 的 Internet 档案快照]

To obtain the full version number for the operating system, call the GetFileVersionInfo function on one of the system DLLs, such as Kernel32.dll, then call VerQueryValue to obtain the \StringFileInfo\\ProductVersion subblock of the file version information. [From an Internet Archive snapshot of MSDN circa 2017]

看起来像这样:

// Quick hack without error checking.
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
#include <Windows.h>

int main() {
  const auto system = L"kernel32.dll";
  DWORD dummy;
  const auto cbInfo =
      ::GetFileVersionInfoSizeExW(FILE_VER_GET_NEUTRAL, system, &dummy);
  std::vector<char> buffer(cbInfo);
  ::GetFileVersionInfoExW(FILE_VER_GET_NEUTRAL, system, dummy,
                          buffer.size(), &buffer[0]);
  void *p = nullptr;
  UINT size = 0;
  ::VerQueryValueW(buffer.data(), L"\\", &p, &size);
  assert(size >= sizeof(VS_FIXEDFILEINFO));
  assert(p != nullptr);
  auto pFixed = static_cast<const VS_FIXEDFILEINFO *>(p);
  std::cout << HIWORD(pFixed->dwFileVersionMS) << '.'
            << LOWORD(pFixed->dwFileVersionMS) << '.'
            << HIWORD(pFixed->dwFileVersionLS) << '.'
            << LOWORD(pFixed->dwFileVersionLS) << '\n';

  return 0;
}

请注意 原始 MSDN 链接 现在重定向到未提及此方法的较新文档集.我想这意味着这不再是一种受支持的技术,而且,据推测,旧代码的所有兼容性黑客可能会阻止应用程序获得实际答案.

Note that the original MSDN link now redirects to a newer documentation set that doesn't mention this approach. I suppose that means this is no longer a supported technique, and, presumably, all the compatibility hacks for older code might prevent an application from getting the actual answer.

选项 2:查询注册表,具体如下:

Option 2: Query the registry, specifically:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

其中包含 CurrentMajorVersionNumberCurrentMinorVersionNumberCurrentBuildNumber 的值.

which has values for CurrentMajorVersionNumber, CurrentMinorVersionNumber, and CurrentBuildNumber.

我找不到这些值的官方文档,因此这可能不是 MSDN 批准的或面向未来的.

I can't find official documentation for these values, so this may not be MSDN-approved or future-proof.

选项 3:使用 GetProductInfo(如果可用),如果不可用则回退到 GetVersionInfo.

Option 3: Use GetProductInfo if available and fall back to GetVersionInfo if it's not.

这篇关于从 Win32 Api C++ 获取操作系统构建版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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