如何以面向未来的方式确定 Windows 版本 [英] How to determine Windows version in future-proof way

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

问题描述

我注意到 GetVersionEx() 已宣布弃用.更糟糕的是,对于 Windows 8.1(以及可能的未来版本),版本号受到应用程序清单的限制.

I noticed that GetVersionEx() is declared deprecated. Worse yet, for Windows 8.1 (and presumably future releases) the version number is limited by the application manifest.

我的目标是收集对用户运行的操作系统的分析,以便我可以适当地定位支持.我想要一个面向未来的解决方案来收集这些数据.更新清单将不起作用,因为我只能为已发布的 Windows 版本更新清单,而不能为未来版本更新.建议的替换 API,版本帮助程序功能,没用.

My goal is to collect analytics on operating systems which the users are running, so I can appropriately target support. I would like a future-proof solution for collecting this data. Updating the manifest won't work because I can only update the manifest for Windows versions which have already been released, not for future versions. The suggested replacement API, the version helper functions, is useless.

如何收集实际的 Windows 版本号?

How can I collect the actual Windows version number?

澄清:通过面向未来",我的意思是我想要一些有相当好的机会在下一个版本的 Windows 上工作的东西.没有什么是确定的,但文档确实说 GetVersionEx() 不起作用.

To clarify: By "future proofing", I just mean that I want something that has a reasonably good chance of working on the next version of Windows. Nothing is certain, but the docs do say that GetVersionEx() won't work.

推荐答案

MSDN 有 一个例子 展示了如何使用(对你的场景没用)版本辅助函数,但在介绍中如下:

MSDN has an example showing how to use the (useless for your scenario) version helper functions, but in the introduction is the following:

要获取操作系统的完整版本号,请对系统 DLL 之一(例如 Kernel32.dll)调用 GetFileVersionInfo 函数,然后调用 VerQueryValue 获取文件版本信息的 StringFileInfo\ProductVersion 子块.

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.

截至目前,GetFileVersionInfoVerQueryValue 函数均已弃用.

As of right now, neither the GetFileVersionInfo nor VerQueryValue function are deprecated.

这将从 kernel32.dll 中提取产品版本并将其打印到控制台:

This will extract the product version from kernel32.dll and print it to the console:

#pragma comment(lib, "version.lib")

static const wchar_t kernel32[] = L"\kernel32.dll";
wchar_t *path = NULL;
void *ver = NULL, *block;
UINT n;
BOOL r;
DWORD versz, blocksz;
VS_FIXEDFILEINFO *vinfo;

path = malloc(sizeof(*path) * MAX_PATH);
if (!path)
    abort();

n = GetSystemDirectory(path, MAX_PATH);
if (n >= MAX_PATH || n == 0 ||
    n > MAX_PATH - sizeof(kernel32) / sizeof(*kernel32))
    abort();
memcpy(path + n, kernel32, sizeof(kernel32));

versz = GetFileVersionInfoSize(path, NULL);
if (versz == 0)
    abort();
ver = malloc(versz);
if (!ver)
    abort();
r = GetFileVersionInfo(path, 0, versz, ver);
if (!r)
    abort();
r = VerQueryValue(ver, L"\", &block, &blocksz);
if (!r || blocksz < sizeof(VS_FIXEDFILEINFO))
    abort();
vinfo = (VS_FIXEDFILEINFO *) block;
printf(
    "Windows version: %d.%d.%d",
    (int) HIWORD(vinfo->dwProductVersionMS),
    (int) LOWORD(vinfo->dwProductVersionMS),
    (int) HIWORD(vinfo->dwProductVersionLS));
free(path);
free(ver);

这篇关于如何以面向未来的方式确定 Windows 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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