File Version / Product Version

Some sample code to get the File / Product Version of a file.

#include <stdio.h>
#include <windows.h>

#ifndef nullptr
 #define nullptr NULL
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    if (argc > 1)
    {
        DWORD dwSize = GetFileVersionInfoSize(argv[1], nullptr);
        if (dwSize > 0)
        {
            BYTE* pBlock = (BYTE*)malloc(dwSize);
            if (pBlock != nullptr)
            {
                if (GetFileVersionInfo(argv[1], 0, dwSize, pBlock))
                {
                    VS_FIXEDFILEINFO* pFixedFileInfo = nullptr;
                    UINT uSize = 0;
                    if (VerQueryValue(pBlock, _T("\\"), (void**)&pFixedFileInfo, &uSize) &&
                        (uSize == sizeof(VS_FIXEDFILEINFO)))
                    {
                        printf("File Version: %d.%d.%d.%d\n",
                                     pFixedFileInfo->dwFileVersionMS >> 16,
                                     pFixedFileInfo->dwFileVersionMS & 0xFFFF,
                                     pFixedFileInfo->dwFileVersionLS >> 16,
                                     pFixedFileInfo->dwFileVersionLS & 0xFFFF);

                        printf("Product Version: %d.%d.%d.%d\n",
                                     pFixedFileInfo->dwProductVersionMS >> 16,
                                     pFixedFileInfo->dwProductVersionMS & 0xFFFF,
                                     pFixedFileInfo->dwProductVersionLS >> 16,
                                     pFixedFileInfo->dwProductVersionLS & 0xFFFF);
                    }
                }
                free(pBlock);
                pBlock = 0;
            }
        }
    }

    return 0;
}