How do I programmatically get the version of a DLL or EXE file?

C++WinapiDllVersionExe

C++ Problem Overview


I need to get the product version and file version for a DLL or EXE file using Win32 native APIs in C or C++. I'm not looking for the Windows version, but the version numbers that you see by right-clicking on a DLL file, selecting "Properties", then looking at the "Details" tab. This is usually a four-part dotted version number x.x.x.x.

C++ Solutions


Solution 1 - C++

You would use the http://msdn.microsoft.com/en-us/library/ms647003.aspx">GetFileVersionInfo</a> API.

See http://msdn.microsoft.com/en-us/library/ms646985(VS.85).aspx">Using Version Information on the MSDN site.

Sample:

DWORD  verHandle = 0;
UINT   size      = 0;
LPBYTE lpBuffer  = NULL;
DWORD  verSize   = GetFileVersionInfoSize( szVersionFile, &verHandle);

if (verSize != NULL)
{
	LPSTR verData = new char[verSize];

	if (GetFileVersionInfo( szVersionFile, verHandle, verSize, verData))
	{
		if (VerQueryValue(verData,"\\",(VOID FAR* FAR*)&lpBuffer,&size))
		{
			if (size)
			{
				VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
				if (verInfo->dwSignature == 0xfeef04bd)
				{

                    // Doesn't matter if you are on 32 bit or 64 bit,
                    // DWORD is always 32 bits, so first two revision numbers
                    // come from dwFileVersionMS, last two come from dwFileVersionLS
                    TRACE( "File Version: %d.%d.%d.%d\n",
                    ( verInfo->dwFileVersionMS >> 16 ) & 0xffff,
                    ( verInfo->dwFileVersionMS >>  0 ) & 0xffff,
                    ( verInfo->dwFileVersionLS >> 16 ) & 0xffff,
                    ( verInfo->dwFileVersionLS >>  0 ) & 0xffff
                    );
				}
			}
		}
	}
    delete[] verData;
}

Solution 2 - C++

All these solutions did not work properly (with my system). I found out that each of the four parts of the version number are saved as a 16-bit value.

The first two numbers are saved in the 32-bit DWORD dwFileVersionMS, and the second two in dwFileVersionLS. So I edited your code at the output section like this:

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

And it works perfectly. The output is formatted like on my system:

major.minor.build.revision

Solution 3 - C++

You get this information using the version information APIs. Here is a sample:

void PrintFileVersion( TCHAR *pszFilePath )
{
    DWORD               dwSize              = 0;
    BYTE                *pbVersionInfo      = NULL;
    VS_FIXEDFILEINFO    *pFileInfo          = NULL;
    UINT                puLenFileInfo       = 0;

    // Get the version information for the file requested
    dwSize = GetFileVersionInfoSize( pszFilePath, NULL );
    if ( dwSize == 0 )
    {
        printf( "Error in GetFileVersionInfoSize: %d\n", GetLastError() );
        return;
    }

    pbVersionInfo = new BYTE[ dwSize ];

    if ( !GetFileVersionInfo( pszFilePath, 0, dwSize, pbVersionInfo ) )
    {
        printf( "Error in GetFileVersionInfo: %d\n", GetLastError() );
        delete[] pbVersionInfo;
        return;
    }

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

    // pFileInfo->dwFileVersionMS is usually zero. However, you should check
    // this if your version numbers seem to be wrong

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

    // pFileInfo->dwProductVersionMS is usually zero. However, you should check
    // this if your version numbers seem to be wrong.

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

Solution 4 - C++

Found these articles...sorry, but I don't have direct experience with how to do this using native APIs, so I deferred to an Internet search:

  • C++: [Determining the version number of a DLL or Executable][1]
  • VB (probably version 6): [How to Use Functions in VERSION.DLL][2]

Hope these help!

[1]: http://www.codeproject.com/KB/DLL/dllversion.aspx "Determining the version number of a DLL or Executable" [2]: http://support.microsoft.com/kb/139491 "How to Use Functions in VERSION.DLL"

Solution 5 - C++

This code shows the file version numbers correctly.

( pFileInfo->dwFileVersionMS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionMS >> 0 ) & 0xff,
( pFileInfo->dwFileVersionLS >>  16 ) & 0xff,
( pFileInfo->dwFileVersionLS >>  0 ) & 0xff);

Solution 6 - C++

The easiest way is to use the GetFileVersionInfoEx or GetFileVersionInfo API functions.

You can also do it from within your application resources as explained here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJSBձոգչView Question on Stackoverflow
Solution 1 - C++crashmstrView Answer on Stackoverflow
Solution 2 - C++Dave ChandlerView Answer on Stackoverflow
Solution 3 - C++JSBձոգչView Answer on Stackoverflow
Solution 4 - C++Steve JView Answer on Stackoverflow
Solution 5 - C++VasyaView Answer on Stackoverflow
Solution 6 - C++Dani van der MeerView Answer on Stackoverflow