Click here to Skip to main content
13,092,310 members (58,497 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 5 May 2010

FileVersion - Retrieve file version information

, 17 May 2010
Rate this:
Please Sign up or sign in to vote.
Easily determine the version of a specified file or the version of the current process.


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.


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;

// Get formated file version
cFileVersion.GetFileVersion( L"Version: %i.%i", strFileVersion );

Useful to keep your About dialog up-to-date:

About Box

Get the version of a specified file

CString        strFileVersion;
FileVersion    cFileVersion( L"C:\\Windows\\System32\\NativeHooks.dll" );

// Get file version
cFileVersion.GetFileVersion( strFileVersion );

MessageBox( strFileVersion );


File version of NativeHooks.dll

Compare file versions

// Version is 6.1.7600.16385
FileVersion    cFileVersionNativeHooks( L"C:\\Windows\\System32\\NativeHooks.dll" );

// Version is 12.0.7600.16385
FileVersion    cFileVersionWmerror( L"C:\\Windows\\System32\\wmerror.dll" );

// Compare versions
bool bIsGreater =  ( cFileVersionNativeHooks > cFileVersionWmerror );  // false
bool bIsLess    =  ( cFileVersionNativeHooks < cFileVersionWmerror );  // true
bool bIsEqual    =  ( cFileVersionNativeHooks == cFileVersionWmerror ); // false



  • explicit FileVersion( const TCHAR * kstrFilePath ): Creates the FileVersion of the specified file.
  • FileVersion() Creates the FileVersion: of the current process.


  • 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:".
  • 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: "".
  • bool IsEqual( const FileVersion & krhs ) const: Returns true if the file version is equal to the passed file version.


  • 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 )
    // Precondition
    if ( NULL == kstrFilePath )
        return false; // FilePath is empty, no file to determine version.

    DWORD    dwHandle;

    // Determines whether the operating system can retrieve version information
    // for a specified file.
    DWORD    dwFileVersionInfoSize = 
        GetFileVersionInfoSize( kstrFilePath, &dwHandle );

    if ( NULL == dwFileVersionInfoSize )
        return false;    // Can't retrieve version information size.

    // Allocate space to retrieve version information using vector to prevent
    // memory leaks
    std::vector<BYTE>    pData( dwFileVersionInfoSize );

    // Retrieves version information for the specified file.
    if ( false == GetFileVersionInfo( kstrFilePath
        , dwHandle
        , dwFileVersionInfoSize
        , static_cast<lpvoid>( &pData[0] ) ) )
        return false; // Can't retrieve version information.

    // The memory of ptFileInfo is freed when pData is freed.
    UINT    uintSize;

    // Retrieves version information from the version-information resource
    if ( false == VerQueryValue( static_cast<lpvoid>( &pData[0] )
        , _T("\\")
        , reinterpret_cast<lpvoid*> ( &ptFileInfo )
        , &uintSize ) )
        return false; // Can't retrieve version information

    // Resolve major, minor, release and build number.
    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;


This class compiles with UNICODE and ANSI at warning level 4.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Martin Brandl
Software Developer
Germany Germany
I study computer science, develop software in my free time.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
rbrunton14-May-11 3:02
memberrbrunton14-May-11 3:02 
Generalincorrect usage of auto_ptr Pin
imagiro6-May-10 0:25
memberimagiro6-May-10 0:25 
GeneralRe: incorrect usage of auto_ptr [modified] Pin
Martin Brandl6-May-10 0:37
memberMartin Brandl6-May-10 0:37 
GeneralReally cool ! Pin
wellental5-May-10 23:19
memberwellental5-May-10 23:19 
GeneralMy vote of 2 Pin
dgendreau5-May-10 17:28
memberdgendreau5-May-10 17:28 
GeneralRe: My vote of 2 [modified] Pin
Martin Brandl5-May-10 21:39
memberMartin Brandl5-May-10 21:39 
GeneralRe: My vote of 2 [modified] Pin
dgendreau6-May-10 5:09
memberdgendreau6-May-10 5:09 
GeneralRe: My vote of 2 [modified] Pin
dgendreau6-May-10 6:57
memberdgendreau6-May-10 6:57 
As an illustration, If I were to keep this as a class, here is a quick sketch of how I might have designed it:

class CFileVersionInfo : public VS_FIXEDFILEINFO
	// Constructors / assignment operator
	explicit CFileVersionInfo( LPCTSTR path );
	CFileVersionInfo( const VS_FIXEDFILEINFO& rv );
	CFileVersionInfo& operator=( const VS_FIXEDFILEINFO& rv );
	// Operations
	BOOL LoadFile( LPCTSTR path );
	// Attributes
	BOOL IsValid() const;
	CString FormatFileVersion( LPCTSTR format=_T("%d.%d.%d.%d") ) const;
	CString FormatProductVersion( LPCTSTR format=_T("%d.%d.%d.%d") ) const;
	CString GetFileFlags() const;
	LPCTSTR GetFileType() const;
	// Comparison
	int CompareFileVersion( const VS_FIXEDFILEINFO& rv ) const;
	int CompareProductVersion( const VS_FIXEDFILEINFO& rv ) const;
	int Compare( const VS_FIXEDFILEINFO& rv ) const;

By deriving from VS_FIXEDFILEINFO, we can provide access to all VS_FIXEDFILEINFO members. Some programmers would require that VS_FIXEDFILEINFO be a private member instead of a parent class and that accessor functions be used to get at members, but for a struct like this I think thats unnecesary.

Convenient string formatting functions are provided to make it easier to get the file and product versions as strings. The class uses CString only when it is required to return a dynamic string.

I would not recommend compare operators in this class since they provide little benefit and can be more difficult to debug. Instead, 3 compare functions similar to CString::Compare are provided.

modified on Thursday, May 6, 2010 1:06 PM

GeneralRe: My vote of 2 Pin
Martin Brandl6-May-10 7:17
memberMartin Brandl6-May-10 7:17 
GeneralRe: My vote of 2 [modified] Pin
dgendreau6-May-10 7:36
memberdgendreau6-May-10 7:36 
GeneralRe: My vote of 2 Pin
Martin Brandl6-May-10 8:19
memberMartin Brandl6-May-10 8:19 
GeneralAwesome one - 5 from me Pin
KunalChowdhury5-May-10 5:55
mentorKunalChowdhury5-May-10 5:55 
GeneralRe: Awesome one - 5 from me Pin
Martin Brandl5-May-10 5:58
memberMartin Brandl5-May-10 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 18 May 2010
Article Copyright 2010 by Martin Brandl
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid