Virtual Developer Workshop: Containerized Development with Docker

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;

  // access version resource

  // in case of success ...
  if (m_info.IsValid())
    // ... refresh dialog data (or do something 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!


Download demo project - 23 Kb
Download source - 11 Kb


  • WAT

    Posted by Manuel L. on 11/08/2018 02:49pm

    It's bad practice, to say the least, to use delete when you used new[]. All the "delete" in that code should be replaced by "delete[]"

  • how to find a hidden file?

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

    Originally posted by: thomas

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

  • Set Fileversion info at runtime

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

    Originally posted by: bosyotik

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

  • great contribution

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

    Originally posted by: narada

    Great contribution. I was looking for this exactly!

  • right Filedate solution

    Posted by Legacy on 09/10/2002 07: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;
    GetFileTime(tfile, NULL, NULL, &m_fileTime);
    m_fixedFileInfo.dwFileDateLS = m_fileTime.dwLowDateTime;
    m_fixedFileInfo.dwFileDateMS = m_fileTime.dwHighDateTime;

    thanks for the code and cya, Daniel

  • Getting your own info

    Posted by Legacy on 08/09/2002 07: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));


    • Thank YOU! :-)

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

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

  • Command line errorlevel

    Posted by Legacy on 05/22/2002 07: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 ???)

  • FYI : version.lib

    Posted by Legacy on 04/17/2002 07: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.

  • Very Nice source!!

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

    Originally posted by: Ai Xing

    Very Nice source!!

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date