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

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds