c++winapiversionexefileversioninfo

Programmatically get the ProductVersion of .exe using GetFileVersionInfo API (C++)


Referencing to this answer I'm trying to get ProductVersion with windows Api using GetFileVersionInfo method. The problem is that through the propreties of .exe ProductVersion is visible, but programmatically I get only "0.0.0.0".

.exe propreties:

exe propreties

output:

output:

Code:

                printf( "File Version 1: %d.%d.%d.%d\n",
                    ( verInfo->dwFileVersionMS >> 16 ) & 0xffff,
                    ( verInfo->dwFileVersionMS >>  0 ) & 0xffff,
                    ( verInfo->dwFileVersionLS >> 16 ) & 0xffff,
                    ( verInfo->dwFileVersionLS >>  0 ) & 0xffff
                    );

                printf( "File Version 2: %d.%d.%d.%d\n",
                    ( verInfo->dwFileVersionLS >> 24 ) & 0xff,
                    ( verInfo->dwFileVersionLS >> 16 ) & 0xff,
                    ( verInfo->dwFileVersionLS >>  8 ) & 0xff,
                    ( verInfo->dwFileVersionLS >>  0 ) & 0xff
                    );


                printf( "Product Version 1: %d.%d.%d.%d\n",
                    ( verInfo->dwProductVersionLS >> 24 ) & 0xff,
                    ( verInfo->dwProductVersionLS >> 16 ) & 0xff,
                    ( verInfo->dwProductVersionLS >>  8 ) & 0xff,
                    ( verInfo->dwProductVersionLS >>  0 ) & 0xff
                    );

                printf( "Product Version 2: %d.%d.%d.%d\n",
                    (verInfo->dwProductVersionMS >> 16) & 0xffff,
                    (verInfo->dwProductVersionMS >>  0) & 0xffff,
                    (verInfo->dwProductVersionLS >> 16) & 0xffff,
                    (verInfo->dwProductVersionLS >>  0) & 0xffff
                    );

                printf( "Product Version 3: %d.%d.%d.%d\n",
                    (verInfo->dwProductVersionMS >> 16) & 0xffff,
                    (verInfo->dwProductVersionMS >>  8) & 0xffff,
                    (verInfo->dwProductVersionLS >> 16) & 0xffff,
                    (verInfo->dwProductVersionLS >>  8) & 0xffff
                    );

The question is - WTF? How to get ProductVersion, and how do the guys from Microsoft did that?


Solution

  • The version info resource contains a small fixed portion (VS_FIXEDFILEINFO) and optionally some strings.

    Some applications display the numbers from the fixed portion and some use the FileVersion/ProductVersion strings.

    You should probably use the string if it is present because it allows the developer to add extra pieces of information like Alpha/Beta etc. and because some people forget to properly set the correct version in the fixed part.

    Use the VerQueryValue function to get a list of languages and the strings...