Visual Studio C++ Libraries GetProductName and GetProductVersion

From Minor Miracle Software
Jump to: navigation, search

In VC++ the only way to extract information from the VS_INFO_VERSION is by the functions below.

// This function is the only way to fetch ProductVersion from
// VS_INFO_VERSION in vc++.
static CString GetProductVersion()
{
    CString strResult;

    char szModPath[ MAX_PATH ];
    szModPath[ 0 ] = '\0';
    GetModuleFileName( NULL, szModPath, sizeof(szModPath) );
    DWORD dwHandle = 0;
    DWORD dwSize = GetFileVersionInfoSize( szModPath, &dwHandle );

	if( dwSize > 0 && dwHandle == 0 )
    {
        BYTE* pbBuf = static_cast<BYTE*>( _malloca( dwSize ) );
        if( GetFileVersionInfo( szModPath, dwHandle, dwSize, pbBuf ) )
        {
            UINT uiSize;
            BYTE* lpb;
            if( VerQueryValue( pbBuf,
                               "\\VarFileInfo\\Translation",
                               (void**)&lpb,
                               &uiSize ) )
            {
                WORD* lpw = (WORD*)lpb;
                CString strQuery;
                strQuery.Format( "\\StringFileInfo\\%04x%04x\\ProductVersion", lpw[ 0 ], lpw[ 1 ] );
                if( VerQueryValue( pbBuf,
                                   const_cast<LPSTR>( (LPCSTR)strQuery ),
                                   (void**)&lpb,
                                   &uiSize ) && uiSize > 0 )
                {
                    strResult = (LPCSTR)lpb;
                }
            }
        }
		_freea( pbBuf );
    }

    return strResult;
}

// This function is the only way to fetch ProductName from
// VS_INFO_VERSION in vc++.
static CString GetProductName()
{
    CString strResult;

    char szModPath[ MAX_PATH ];
    szModPath[ 0 ] = '\0';
    GetModuleFileName( NULL, szModPath, sizeof(szModPath) );
    DWORD dwHandle = 0;
    DWORD dwSize = GetFileVersionInfoSize( szModPath, &dwHandle );

	if( dwSize > 0 && dwHandle == 0 )
    {
        BYTE* pbBuf = static_cast<BYTE*>( _malloca( dwSize ) );
        if( GetFileVersionInfo( szModPath, dwHandle, dwSize, pbBuf ) )
        {
            UINT uiSize;
            BYTE* lpb;
            if( VerQueryValue( pbBuf,
                               "\\VarFileInfo\\Translation",
                               (void**)&lpb,
                               &uiSize ) )
            {
                WORD* lpw = (WORD*)lpb;
                CString strQuery;
                strQuery.Format( "\\StringFileInfo\\%04x%04x\\ProductName", lpw[ 0 ], lpw[ 1 ] );
                if( VerQueryValue( pbBuf,
                                   const_cast<LPSTR>( (LPCSTR)strQuery ),
                                   (void**)&lpb,
                                   &uiSize ) && uiSize > 0 )
                {
                    strResult = (LPCSTR)lpb;
                }
            }
        }
		_freea( pbBuf );
    }

    return strResult;
}


Internal Links

Parent Article: Visual Studio C++ Libraries