c++winapifileversioninfo

Accessing a string from the RC file?


I have this .rc file that contains the versions, description etc that is being used for the exe file's details.

How do I get the values for use inside the code? For example, I want to get the ProductName.

IDI_ICON1   ICON    DISCARDABLE "abc-logo.ico"

#if defined(UNDER_CE)
#include <winbase.h>
#else
#include <winver.h>
#endif

VS_VERSION_INFO VERSIONINFO
FILEVERSION 9, 9, 9, 9
PRODUCTVERSION 12, 01, 15, 1
FILEFLAGSMASK 0x3fL
FILEFLAGS 0
FILEOS VOS_NT_WINDOWS32
FILETYPE VFT_APP
FILESUBTYPE VFT2_UNKNOWN

BEGIN
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x0404, 1200
    END
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040404b0"
        BEGIN
            VALUE "CompanyName", "Company A\0"
            VALUE "FileDescription", "Software A\0"
            VALUE "FileVersion", "1.0.0.0\0"
            VALUE "InternalName", "SoftwareX\0"
            VALUE "LegalCopyright", "Copyright (C) Software Inc. All Rights Reserved\0"
            VALUE "OriginalFilename", "SoftwareA.exe\0"
            VALUE "ProductName", "Software A\0"
            VALUE "ProductVersion", "1.1.0.0\0"
        END
    END
END

Solution

  • Use the GetFileVersionInfo() and VerQueryValue() functions, eg:

    TCHAR FileName[MAX_PATH];
    GetModuleFileName(NULL, FileName, MAX_PATH);
    
    DWORD Handle;
    DWORD Size = GetFileVersionInfoSize(FileName, &Handle);
    if (Size == 0)
    {
        // error handling ...
    }
    
    std::vector<BYTE> VersionData(Size);
    if (!GetFileVersionInfo(FileName, Handle, Size, VersionData.data()))
    {
        // error handling ...
    }
    
    LPTSTR Value;
    UINT ValueLen;
    
    if (VerQueryValue(VersionData.data(), TEXT("\\StringFileInfo\\040404b0\\ProductName"), (LPVOID*) &Value, &ValueLen))
    {
        // use Value up to ValueLen chars as needed...
    }
    else
    {
        // error handling ...
    }
    
    if (VerQueryValue(VersionData.data(), TEXT("\\StringFileInfo\\040404b0\\LegalCopyright"), (LPVOID*) &Value, &ValueLen))
    {
        // use Value up to ValueLen chars as needed...
    }
    else
    {
        // error handling ...
    }