2

I want to get the full version number for Windows, just like CMD does:

Full version number from console

I ended up with this MS doc that says:

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.

So I tried to use those functions with this code:

#include <Windows.h>
#include <wchar.h>

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

int wmain(int argc, wchar_t* argv[])
{
    // GetFileVersionInfoW
    LPCWSTR fileName = L"C:\\Windows\\System32\\kernel32.dll";
    DWORD fileInfoSize;

    fileInfoSize = GetFileVersionInfoSizeW(fileName, NULL);

    if (fileInfoSize == 0)
    {
        fwprintf(stderr, L"\nError code: %u\n", GetLastError());
        return;
    }

    // GetFileVersionInfoW
    VOID* pFileVerInfo = malloc(fileInfoSize);

    if (pFileVerInfo == NULL)
    {
        fwprintf(stderr, L"Failed allocating!\n");
        return;
    }

    if (!GetFileVersionInfoW(fileName, 0, fileInfoSize, pFileVerInfo))
    {
        fwprintf(stderr, L"Error code: %u\n", GetLastError());
        free(pFileVerInfo);
        return;
    }

    // VerQueryValueW
    LPCWSTR subBlock = L"\\StringFileInfo\\\\ProductVersion";
    VS_FIXEDFILEINFO * pFileInfo;
    UINT pLen = 0;

    if (!VerQueryValueW(pFileVerInfo, subBlock, (LPVOID*)& pFileInfo, &pLen))
    {
        fwprintf(stderr, L"Error code: %u\n", GetLastError());
        return;
    }


    return 0;
}

However, the VerQueryValueW function fails with code 1813 and I have no idea why. I also have no idea how I can show the full version after calling the function.

Can you help me?

Sergio Calderon
  • 837
  • 1
  • 13
  • 32
  • Try outputting different text for the different failure cases... you can't tell whether it was `GetFileVersionInfoSizeW` that failed, or `GetFileVersionInfoW`, or `VerQueryValueW` – M.M Feb 01 '19 at 03:08
  • Well... I set some breakpoints from within Visual Studio and turns out that `VerQueryValueW` is the one who is failing. – Sergio Calderon Feb 01 '19 at 03:13
  • 1
    `return;` is illegal in function returning `int` – M.M Feb 01 '19 at 03:16
  • 1
    You should look at [this question instead](https://stackoverflow.com/q/36543301/62576) rather than jumping through hoops. – Ken White Feb 01 '19 at 03:20
  • Error 1813 is ["The specified resource type cannot be found in the image file."](https://learn.microsoft.com/en-us/windows/desktop/debug/system-error-codes--1700-3999-) – user253751 Feb 01 '19 at 03:24
  • maybe you also have to get the right region code; on my other working file `\\StringFileInfo\\040904E4\\ProductVersion` works, but it fails to omit the region code – M.M Feb 01 '19 at 03:31
  • Yes you will need the region code as @M.M suggests. You can get it from `\\VarFileInfo\\Translation`. Or, just read the `VS_FIXEDFILEINFO` structure instead. – Jonathan Potter Feb 01 '19 at 03:32

1 Answers1

3

L"\\StringFileInfo\\\\ProductVersion" is not correct. There must be a Language ID in the middle. On my Windows 10 installation, a working string is: L"\\StringFileInfo\\040904B0\\ProductVersion". But maybe this would differ on other systems.

As suggested by Jonathan Potter in comments, you could find the ID by querying \\VarFileInfo\\Translation.


Simpler options to achieve the goal include:

M.M
  • 138,810
  • 21
  • 208
  • 365