. Home Feedback Contents Search

Version 

Back Up

Reading Version From Resources

Windows files have a version information structure attached to the resources. Getting at it isn’t hard but, like a lot of things in Windows, it isn’t intuitively obvious, either. Here’s how you can extract the revision and build number from your version information.

First of all, you have to add “Version.lib” to the list of included libraries in the project’s link line. Otherwise, you’ll end up with some unresolved externals at build time.

Here’s the code that will actually read the version information.

//////////////////////////////////////////////////////////////////////////
//
//      CString csGetRevisionAndBuild()
//
//  Description:
//
//      Retrieve the revision and build numbers as a formated string.
//
//  Input:
//
//      none.
//
//  Output:
//
//      CString returned as "Revision xxx.xxx Build xxx"
//
//////////////////////////////////////////////////////////////////////////

CString csGetRevisionAndBuild()
    {
    BYTE*     lpBuff = 0;
    CString   csReturn;

    do {

        // Get the name of this module.

        CString strPath;
        GetModuleFileName( 0, strPath.GetBuffer(_MAX_PATH), _MAX_PATH);
        strPath.ReleaseBuffer();

        // How big a buffer is needed to receive file version info?

        DWORD dwArg;
        DWORD dwInfoSize = GetFileVersionInfoSize( (LPTSTR)(LPCTSTR)strPath, &dwArg);
        if(0 == dwInfoSize)
            break;
        
        lpBuff = new BYTE[dwInfoSize];
        if(!lpBuff)
            break;
        
        // Extract the file version from the specified file's resources.

        if(0 == GetFileVersionInfo( (LPTSTR)(LPCTSTR)strPath, 0, dwInfoSize, lpBuff))
            break;
        
        // Extract that portion of the resources that are specifically involved with
        // revision and build.

        VS_FIXEDFILEINFO *vInfo;
        UINT uInfoSize;
        if (0 == VerQueryValue( lpBuff, TEXT("\\"), (LPVOID*)&vInfo, &uInfoSize))
            break;
        
        if ( !uInfoSize )
            break;
        
        int iRevMajor = HIWORD( vInfo->dwProductVersionMS );
        int iRevMinor = LOWORD( vInfo->dwProductVersionMS );
        int iBuild    = LOWORD( vInfo->dwFileVersionLS);

      csReturn.Format( _T("Revision %d.%d Build %d"), iRevMajor, iRevMinor, iBuild );

        } while (false);

    if ( lpBuff )
        delete lpBuff;

    return csReturn;
    }

Back Up