Search code examples
c++winapidllversionexe

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


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.


Solution

  • You would use the GetFileVersionInfo API.

    See 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;
    }