使用GetFileVersionInfo API(C ++)以编程方式获取.exe的ProductVersion

时间:2017-08-04 10:37:25

标签: c++ winapi version exe fileversioninfo

参考此answer我试图使用GetFileVersionInfo方法使用Windows Api获取ProductVersion。问题是通过.exe ProductVersion的可行性是可见的,但是通过编程我只得到" 0.0.0.0"。

.exe propreties:

exe propreties

输出:

output:

代码:

                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
                    );

问题是 - WTF?如何获得ProductVersion,以及微软的人如何做到这一点?

2 个答案:

答案 0 :(得分:1)

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...

答案 1 :(得分:0)

以下是那些同时尝试获取ProductVersion的代码段:

if (!VerQueryValue (lpData, TEXT("\\StringFileInfo\\040904E4\\ProductVersion"),
        (LPVOID) &lpBuffer, (PUINT) &BufLen)) {
    /* language ID 040904E4: U.S. English, char set = Windows, Multilingual */
    printf ("ProductVersion:       not found\n");
}
else
    printf ("ProductVersion:       %s\n", lpBuffer);

这是Full Code