Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine Windows version in future-proof way

Tags:

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.

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.

How can I collect the actual Windows version number?

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.

like image 233
Dietrich Epp Avatar asked Sep 23 '14 03:09

Dietrich Epp


1 Answers

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

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.

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

Example

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);
like image 61
ta.speot.is Avatar answered Sep 24 '22 02:09

ta.speot.is