Click here to Skip to main content
15,914,074 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
2.44/5 (8 votes)
1 Jan 20071 min read 27.7K   568   14   2
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


Written By
India India
label4: C,C++,MFC,ATL,COM,C#

Comments and Discussions

 
GeneralSet file Info instead of Geting it Pin
Daniel Ortega18-Jan-07 23:35
Daniel Ortega18-Jan-07 23:35 
AnswerRe: Set file Info instead of Geting it Pin
Masoom Shaikh19-Jan-07 0:07
Masoom Shaikh19-Jan-07 0:07 

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.