Class Wrapper to Retrieve Module Information (VS_VERSION_INFO Resource)

Environment:Win 95, Win 98 SE, NT4 SP3, Win 2000

Yet another class to get all the VS_VERSION_INFO resource information you need, from any module!

Many people have requested this code in response to reading some of my posts - well, here it is. You can optionally specify language id and code page, to query language specific VS_VERSION_INFO elements of the block - very helpful. You have the option of querying your own module information (many people have been confused as to how to do this). The code to get your own module name, I have included in a separate class (CVFXGetModuleName) so that you can use it separately (for whatever reason). The retrieved values, are also returned in a separate class (CVFXVersionRes) making it easy to pass the retrieved information around as an object.

Feel free to improve upon it if you find it lacking in any area - enjoy!

//-----------------------------------------------------------------
// Here are two examples of implementing and using these classes
//-----------------------------------------------------------------
//
// Make sure to include the following source files into your project:
//
// - VFXGetModuleName.h 
// - VFXGetModuleName.cpp
//
// - VFXPointer.h 
// - VFXPointer.cpp
//
// - VFXVersionInfo.h
// - VFXVersionInfo.cpp
//
// ...Then, #include "VFXVersionInfo.h" in the module you will use
// the class
//
// All the information you will need will be returned in the
// CVFXVersionRes class object pass to the 
// CVFXVersionInfo::GetVersionInfo(CVFXVersionRes&) member
//

//-----------------------------------------------------------------
// Example to get your OWN module's version information
//-----------------------------------------------------------------
bool CMySillyClass::GetThisModuleVerInfo(CString& sVersion)
   {
   sVersion.Empty();

   CVFXVersionRes VersionRes;
   CVFXVersionInfo VersionInfo;

   // Note: passing NULL as a module name here will return the 
   // version information for the current module
   if(VersionInfo.GetVersionInfo(VersionRes))
   {
    // Display the whole VS_VERSION_INFO block in a message box
       VersionRes.Display();

        // Get the file version to return
        sVersion = VersionRes.m_sFileVersion;

        return true;
   }
   else
   {
    AfxMessageBox("Module not found, or version info not found",
                  MB_OK | MB_ICONERROR);
   }

   return false;
   }

//-----------------------------------------------------------------
// Example to get your ANOTHER module's version information
//-----------------------------------------------------------------
bool CMySillyClass::GetAnotherModuleVerInfo(const CString& sModName,
                                            CString& sVersion)
   {
   sVersion.Empty();

   if(sModName.IsEmpty())
      return false;

   CVFXVersionRes VersionRes;
   CVFXVersionInfo VersionInfo;

   // Note: passed a module name here, the default (NULL) will
   //  return the version information for the current module
   if(VersionInfo.GetVersionInfo(VersionRes, sModName))
   {
     // Display the whole VS_VERSION_INFO block in a message box
      VersionRes.Display();

        // Get the file version to return
        sVersion = VersionRes.m_sFileVersion;

        return true;
   }
   else
   {
    AfxMessageBox("Module not found, or version info not found",
                  MB_OK | MB_ICONERROR);
   }

   return false;
   }

//-----------------------------------------------------------------
// Example to get your OWN module's version information (UK block)
//-----------------------------------------------------------------
bool CMySillyClass::GetThisModuleVerInfoUKBlock(CString& sVersion)
   {
   sVersion.Empty();

   CVFXVersionRes VersionRes;
   // Notice passing codepage and language id specific - 
   //   default is English US!
   CVFXVersionInfo VersionInfo(ENGLISH_UNICODE_UK, 
                               MAKELANGID(LANG_ENGLISH,
                                          SUBLANG_ENGLISH_UK));

   // Note: passing NULL as a module name here will return the 
   // version information for the current module
   if(VersionInfo.GetVersionInfo(VersionRes))
   {
     // Display the whole VS_VERSION_INFO block in a message box
      VersionRes.Display();

        // Get the file version to return
        sVersion = VersionRes.m_sFileVersion;

        return true;
   }
   else
   {
     AfxMessageBox("Module not found, or version info not found",
                   MB_OK | MB_ICONERROR);
   }

   return false;
   }

Downloads

Download demo project - 12 Kb
Download source - 20 Kb


Comments

  • why not just use the simple windows apis for this?

    Posted by fransn on 09/04/2008 12:07pm

    string GetVersion(LPCSTR szFile) { BOOL bOK = TRUE ; std::ostringstream strmVersion ; DWORD dwHandle, dwLen; UINT BufLen; LPTSTR lpData = NULL ; VS_FIXEDFILEINFO* pFileInfo = NULL ; if(bOK) { dwLen = GetFileVersionInfoSize( (LPSTR)szFile, &dwHandle ); bOK = (dwLen > 0) ; } if(bOK) { lpData = (LPTSTR) malloc (dwLen); bOK = (lpData != NULL) ; } if(bOK) { bOK = GetFileVersionInfo( (LPSTR)szFile, dwHandle, dwLen, lpData ) ; } if(bOK) { bOK = VerQueryValue( lpData, "\\", (void**) &pFileInfo, (PUINT)&BufLen ) ; } if(bOK) { strmVersion<dwFileVersionMS)<<"."<dwFileVersionMS)<<"."<dwFileVersionLS)<<"."<dwFileVersionLS) ; strmVersion<<"\r\n"; strmVersion<dwProductVersionMS)<<"."<dwProductVersionMS)<<"."<dwProductVersionLS)<<"."<dwProductVersionLS) ; } if(lpData ) { free (lpData); } return strmVersion.str();; }

    Reply
  • Can you tell me: why i can't post or replay in forum, since i have registerd in codecuru?

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

    Originally posted by: sniffer_chang

    Can you tell me: why i can't post or replay in forum, since i have registerd in codecuru?

    my E_mail: sniffer_chang@hotmail.com

    Reply
  • Problems compiling or running the demo? Look here!

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

    Originally posted by: Gene M. Angelo

    1) To fix any compiler errors when compiling (can't find psapi.h), download the latest Platform SDK from Microsoft:

    http://www.microsoft.com/msdownload/platformsdk/sdkupdate/

    After download and installation, be sure to register the PSDK directories with Visual Studio from the Windows "Start | Programs | Microsoft Platform SDK August 2001 (or whatever date) | Visual Studio Registration" menu.

    2) If you experience loading errors running under Win 9x. It appears to be from the use of the GetModuleFileNameEx() API in the CVFXGetModuleName class which is unavailable under Win 9x. I have confirmed that the class works under Win NT and 2000. I will be looking into a fix for Win 9x users (yuck!) :)


    Sorry for the hassel

    Reply
  • Thanks

    Posted by Legacy on 09/09/2001 12:00am

    Originally posted by: Artem

    .

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds