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


Comments

  • how to find a hidden file?

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: thomas

    how to find a hidden file?(in cfiledialog)THANKS

    Reply
  • Set Fileversion info at runtime

    Posted by Legacy on 03/23/2003 12:00am

    Originally posted by: bosyotik

    Very nice code, but how can we set the file version information at runtime or through code?

    Reply
  • great contribution

    Posted by Legacy on 02/04/2003 12:00am

    Originally posted by: narada

    Great contribution. I was looking for this exactly!

    Reply
  • right Filedate solution

    Posted by Legacy on 09/10/2002 12:00am

    Originally posted by: Daniel Haas

    I had difficulties with the right File-Date and so I changed the code a little bit:

    in the function "ReadVersionInfo(const CString& strFile)" insert the code after "memcpy(&m_dwTransInfo, lpVoid, sizeof(DWORD));"

    FILETIME m_fileTime;
    HANDLE tfile = CreateFile(strFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL);
    GetFileTime(tfile, NULL, NULL, &m_fileTime);
    CloseHandle(tfile);
    m_fixedFileInfo.dwFileDateLS = m_fileTime.dwLowDateTime;
    m_fixedFileInfo.dwFileDateMS = m_fileTime.dwHighDateTime;

    thanks for the code and cya, Daniel

    Reply
  • Getting your own info

    Posted by Legacy on 08/09/2002 12:00am

    Originally posted by: Joe Sonderegger

    This code is excellent...
    
    Just an addition:
    By setting adding the following lines you can extract data about yourself...

    char pcFile[MAX_PATH];
    GetModuleFileName(GetModuleHandle(0), pcFile, sizeof(pcFile));

    m_info.ReadVersionInfo(pcFile);

    • Thank YOU! :-)

      Posted by turkinz on 12/29/2005 02:12pm

      This is exactly what I was looking for. BTW, instead of this: "GetModuleHandle(0)", we could put just "NULL".

      Reply
    Reply
  • Command line errorlevel

    Posted by Legacy on 05/22/2002 12:00am

    Originally posted by: Scott

    Does anyone know of a command line utility which can be used under Win2k that can be used to check file version and return and errorlevel (ie. like an IF statement ???)

    Reply
  • FYI : version.lib

    Posted by Legacy on 04/17/2002 12:00am

    Originally posted by: Tony

    Just a reminder not to forget to link version.lib in the project setting.

    Other than that great code, I have been fighting with the raw MSDN way and I love this solution.

    Reply
  • Very Nice source!!

    Posted by Legacy on 04/16/2002 12:00am

    Originally posted by: Ai Xing

    Very Nice source!!
    Thanks.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds