Accessing a File’s Version Resource Information

Environment: VC6 SP5, Win2K SP2

In my company I am developing and maintaining various software applications that are mutually independant, but interrelated. In some situations I need to check the version of some exe or dll, either to see if a certain functionality would be available or for simply displaying the version info. Also it can be very useful to have the opportunity to check a system Dlls (e.g. COMCTL32) version number on the fly.

The WIN32 API provides the so called “File Installation Library” function set to access a files version resource block and extract the data you need. It took me only half of a second looking at the related MSDN documentation to see that working with these APIs in raw form would be too hard a punishment for a C++ developer. So I decided to create a C++ style wrapper that hides this API mess and lets you extract all available information from a files version resource block in a very handy and straightforward manner:

CFileVersionInfo.
Most of CFileVersionInfo‘s attribute functions are inline, so extracting version information from a file is not only an easy thing to do, but is also very fast.

The screenshot above shows the sample application I wrote to demonstrate the use of
CFileVersionInfo and gives you an impression of what kind of data can be retrived from a file. You just have to select a file for which version information is available (eg. *.exe, *.dll or *.sys) either by clicking the “…” button and then selecting a file or by entering the path directly and then pressing Enter. The dialog then will display all available infos, if any. (The “Name” list box on the left and the related “Value” text box work just like the respective controls on the standard Windows “Version” tab from the “Properties” dialog…).

As I already mentioned, the use of CFileVersionInfo is very easy: First make sure that the version.lib library is added to the project settings. After declaring a variable of type CFileVersionInfo, you just have to call the ReadVersionInfo member function, which takes a path to a file as input parameter and returns TRUE in case of success. Another way is to call ReadVersionInfo and then calling the IsValid member function to see if the operation succeeded. Afterwards CFileVersionInfo‘s various attribute member functions can be called to retrieve the information you need.

The following is a code snippet from the ResView sample application that demonstrates this approach (m_info is a variable of type CFileVersionInfo):


void CResViewDlg::Refresh()
{
// get path name
CString strFile;
m_editFile.GetWindowText(strFile);

// access version resource
m_info.ReadVersionInfo(strFile);

// in case of success …
if (m_info.IsValid())
{
// … refresh dialog data (or do something else)
}
else
{
// … alert the user (or do nothing)
}
}

For a detailed list of information pieces that can be accessed by CFileVersionInfo member functions you can check out the sample application
(especially the CResViewDlg::Refresh() member function) and/or examine the
extensive and detailed comments sections (including hints for related information
in the MSDN documentation where appropriate) I included with the CFileVersionInfo source files. (I decided to put the inline functions in a separate (*.inl) file for the sake of clearness).

Note:
Because I wrote CFileVersionInfo mainly to check a file’s version and to access some of the resouce’s text entries, I did not test the code very extensively. So don’t be too surprised if you find something in my code that’s not working the way you expect it to do!

Downloads

Download demo project – 23 Kb

Download source – 11 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read