Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Version reading component (as in Explorer's Version tab)

2.44/5 (8 votes)
2 Jan 20071 min read 2   570  
Version APIs wrapped in a simple component.

Sample Image

Introduction

This article presents a simple COM object to read the version information of a given file. The version information returned is the same as what appears in the Version tab of file properties in Windows Explorer.

Background

It is fairly easy to use this component. Initially, I designed this code in C++ as a class, with a constructor taking a filename as the argument. Then, I decided to make it a COM component to cater to a wider audience (and also so that I can use it in scripts). It can be used in any language that supports COM but if you are interested in understanding the Win32 version API, then you may want to look for the following functions in MSDN: GetFileVersionInfoSize(), GetFileVersionInfo(), and VerQueryValue().

Using the code

Any one familiar with using COM components in VB6, .NET, or C++ shall be able to use this code. I have provided a working C++ version which will simply print the version of a file it receives on the command line in CSV (comma separated values) format. I then use this program in a batch file, using the 'for' command to generate a *.csv file for all versions of drivers located in windows\system32\drivers. I like the *.csv format since it is the easiest way to put your data in Microsoft Excel (or any other program you use for formatting output to a more appealing one). Currently, only fields mentioned in MSDN are programmed, but it is easy to build your own version by adding entries in the VersionFieldArray and adding another enumeration value in VersionFieldEnum. Then, add a property for the same.

// use this array to extend for more fields
const tstring VersionFieldArray[] = { 
                                        _T("Comments"), 
                                        _T("CompanyName"), 
                                        _T("FileDescription"), 
                                        _T("FileVersion"), 
                                        _T("InternalName"), 
                                        _T("LegalCopyright"), 
                                        _T("LegalTrademarks"), 
                                        _T("OriginalFilename"), 
                                        _T("PrivateBuild"), 
                                        _T("ProductName"), 
                                        _T("ProductVersion"), 
                                        _T("SpecialBuild")
                                    };
// use this enumeration along with above array
enum VersionFieldEnum
{
    Comments,
    CompanyName,
    FileDescription,
    FileVersion,
    InternalName,
    LegalCopyright,
    LegalTrademarks,
    OriginalFilename,
    PrivateBuild, 
    ProductName,
    ProductVersion,
    SpecialBuild,
    MaxVersionFields
};

History

1 Jan 2007 - updated downloads

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here