Introduction
Getting the version information of a file is useful in many cases. Maybe you want to check the version of a library before you load it, to ensure not loading an old one which can result in unexpected software behaviour. Or you simply want to display the current version of your application in the About-box. This class allows you to do that in a very easy way.
Background
I'm going to develop an application which detects problems with duplicated libraries. It should notify me if an old library has been loaded instead of the new one because of the "Library Search Order". This class provides me the basic requirements for that, like determining or comparing file versions.
There are similar articles on the CodeProject already. I developed my own class to get a lightweight way to determine file versions without storing other information like company name, description, etc..
Using the code
The following samples demonstrate how to use this class.
Get the version of the current process
CString strFileVersion;
FileVersion cFileVersion;
cFileVersion.GetFileVersion( L"Version: %i.%i", strFileVersion );
Useful to keep your About dialog up-to-date:
Get the version of a specified file
CString strFileVersion;
FileVersion cFileVersion( L"C:\\Windows\\System32\\NativeHooks.dll" );
cFileVersion.GetFileVersion( strFileVersion );
MessageBox( strFileVersion );
Output:
Compare file versions
FileVersion cFileVersionNativeHooks( L"C:\\Windows\\System32\\NativeHooks.dll" );
FileVersion cFileVersionWmerror( L"C:\\Windows\\System32\\wmerror.dll" );
bool bIsGreater = ( cFileVersionNativeHooks > cFileVersionWmerror ); bool bIsLess = ( cFileVersionNativeHooks < cFileVersionWmerror ); bool bIsEqual = ( cFileVersionNativeHooks == cFileVersionWmerror );
Interface
Constructors
explicit FileVersion( const TCHAR * kstrFilePath )
: Creates the FileVersion
of the specified file.FileVersion()
Creates the FileVersion
: of the current process.
Methods
bool IsValid() const
: Returns whether the file version could be determined.unsigned short GetMajorVersion() const
: Returns the Major Version.unsigned short GetMinorVersion() const
: Returns the Minor Version.unsigned short GetReleaseNumber() const
: Returns the Release Number.unsigned short GetBuildNumber() const
: Returns the Build Number.bool GetFileVersion( const TCHAR* kstrFileFormat, CString& strFileVersion ) const
: Gets the file version string. The order is Major, Minor, Release, and Build Number. The string can be formatted; for instance, L"Version: %i.%i.%i.%i"
will return a string like "Version: 12.1.0.530".bool GetFileVersion( CString& strFileVersion ) const
: Gets the file version string. The order is Major, Minor, Release, and Build number, with a dot in between. For example: "12.1.0.530".bool IsEqual( const FileVersion & krhs ) const
: Returns true
if the file version is equal to the passed file version.
Operations
bool operator<( const FileVersion & krhs ) const
: Returns true
if the file version is less than the passed file version.bool operator>( const FileVersion & krhs ) const
: Returns true
if the file version is greater than the passed file version.bool operator==( const FileVersion & krhs ) const
: Returns true
if the file version is equal to the passed file version.
Implementation details
This is how the file version is determined:
bool FileVersion::DetermineFileVersion( const TCHAR* kstrFilePath )
{
if ( NULL == kstrFilePath )
return false;
DWORD dwHandle;
DWORD dwFileVersionInfoSize =
GetFileVersionInfoSize( kstrFilePath, &dwHandle );
if ( NULL == dwFileVersionInfoSize )
return false;
std::vector<BYTE> pData( dwFileVersionInfoSize );
if ( false == GetFileVersionInfo( kstrFilePath
, dwHandle
, dwFileVersionInfoSize
, static_cast<lpvoid>( &pData[0] ) ) )
return false;
VS_FIXEDFILEINFO *ptFileInfo;
UINT uintSize;
if ( false == VerQueryValue( static_cast<lpvoid>( &pData[0] )
, _T("\\")
, reinterpret_cast<lpvoid*> ( &ptFileInfo )
, &uintSize ) )
return false;
musMajorVersion = static_cast<unsigned>(
( ptFileInfo->dwFileVersionMS >> 16 ) &0xffff );
musMinorVersion = static_cast<unsigned>(
ptFileInfo->dwFileVersionMS &0xffff );
musReleaseNumber = static_cast<unsigned>(
( ptFileInfo->dwFileVersionLS >> 16 ) &0xffff);
musBuildNumber = static_cast<unsigned>(
ptFileInfo->dwFileVersionLS & 0xffff );
return true;
}
Notes
This class compiles with UNICODE and ANSI at warning level 4.