how to use the GetFileVersionInfo function?

2020-07-27 02:15发布

问题:

I have this code - that is returning the file version (into a struct) I'm using as example the shell32.dll but there are some values that I don't understand their meanings , and would love to get an explanation.

here is the code :

void GetFileVersion( PCHAR pFilePath  ,PVERSION pRetVersion)
{
DWORD               dwSize              = 0;
BYTE                *pVersionInfo       = NULL;
VS_FIXEDFILEINFO    *pFileInfo          = NULL;
UINT                pLenFileInfo        = 0;


/*getting the file version info size */
dwSize = GetFileVersionInfoSize( pFilePath, NULL );
if ( dwSize == 0 )
{
    printf( "Error in GetFileVersionInfoSize: %d\n", GetLastError() );
    return;
}

pVersionInfo = new BYTE[ dwSize ]; /*allocation of space for the verison size */

if ( !GetFileVersionInfo( pFilePath, 0, dwSize, pVersionInfo ) ) /*entering all info     data to pbVersionInfo*/
{
    printf( "Error in GetFileVersionInfo: %d\n", GetLastError() );
    delete[] pVersionInfo;
    return;
}

if ( !VerQueryValue( pVersionInfo, TEXT("\\"), (LPVOID*) &pFileInfo, &pLenFileInfo ) )
{
    printf( "Error in VerQueryValue: %d\n", GetLastError() );
    delete[] pVersionInfo;
    return;
}


/*checking if the allocation succeeded */
if (NULL == pRetVersion)
{
    printf("Allocation failed! \n" , GetLastError());
    return;
}


pRetVersion->major  =  ( pFileInfo->dwFileVersionMS >> 16 ) & 0xffff ;
pRetVersion->minor  =  ( pFileInfo->dwFileVersionMS) & 0xffff;
pRetVersion->hotfix =  ( pFileInfo->dwFileVersionLS >>  16 ) & 0xffff;
pRetVersion->other  =  ( pFileInfo->dwFileVersionLS) & 0xffff;      

}
  1. what is the the meaning of dwSize ? is this only the file version size? where can I see it while clicking on the shell32.dll?

  2. pLenFileinfo - what is this size ?

  3. when I look at the struct of VS_FIXEDFILEINFO there is only the version info information , Is there a wae to get for example : File description , Date modified , Original filename etc ? (all the other properties that are inside the "Details" )?

thanks !!!!!

回答1:

The file version information is of variable length. It contains a number of different pieces of information. The total length of all these different pieces is given by the return value of GetFileVersionInfoSize.

When you call VerQueryValue you are asking for a specific individual piece of information within the overall version information. And the length of that specific part can never be larger that the overall size.

The documentation for VerQueryFile contains sample code that extracts the file description.