CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2004
    Location
    England
    Posts
    90

    Question How do I get my App's version info

    Hi all,

    I have developed an application with MS VC++ 6.0
    I have set the version resource and want to know how to acquire this version info in my application so I can display it in a dialog. Is there an API for this, my search of msdn failed... any assistance will be much obliged

    thanks
    The most knowledgeable people are those who know that they know nothing.

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    To retrieve the version information you can use the following functions...
    Code:
    DWORD GetFileVersionInfoSize(LPTSTR lptstrFilename, LPDWORD lpdwHandle)
    
    BOOL GetFileVersionInfo(LPTSTR lptstrFilename,
                            DWORD  dwHandle,
                            DWORD  dwLen,
                            LPVOID lpData)
    
    BOOL VerQueryValue(const LPVOID pBlock,
                       LPTSTR       lpSubBlock,
                       LPVOID       *lplpBuffer,
                       PUINT        puLen)
    
    DWORD VerLanguageName(DWORD wLang, LPTSTR szLang, DWORD nSize)
    'GetFileVersionInfoSize()' returns the size of the version information. 'GetFileVersionInfo()' uses information retrieved by 'GetFileVersionInfoSize()' to retrieve a structure that contains the version information. 'VerQueryValue()' retrieves a specific member from that structure. Your setup program needs to call those function depending on the error code returned by e.g. 'VerInstallFile()'.

    The following is an example class which wraps the whole thing...
    Code:
    #ifndef VERSION
    #define VERSION
    
    #pragma warning(disable : 4505 4710)
    #pragma comment(lib, "version.lib")
    
    #include <string>
    #include <sstream>
    #include <iomanip>
    #include <exception>
    #include <new>
    #include <windows.h>
    
    
    namespace Version
    {
      struct Language
      {
        WORD m_wLanguage;
        WORD m_wCodePage;
    
        Language()
        {
          m_wLanguage = 0;
          m_wCodePage = 0;
        }
      };
    }
    
    
    class CVersion
    {
    public:
      CVersion()
      {
        // Get application name
        TCHAR szBuffer[MAX_PATH] = "";
    
        if(::GetModuleFileName(0, szBuffer, sizeof(szBuffer) - 1))
        {
          std::string strAppName = szBuffer;
          strAppName             = strAppName.substr(strAppName.rfind("\\") + 1);
    
          // Get version info
          DWORD dwHandle = 0;
    
          DWORD dwResourceSize = ::GetFileVersionInfoSize(const_cast<char *>(strAppName.c_str()), &dwHandle);
          if(dwResourceSize)
          {
            m_pucResourceData = new unsigned char[dwResourceSize];
            if(m_pucResourceData)
            {
              if(::GetFileVersionInfo(const_cast<char *>(strAppName.c_str()),
                                      0,
                                      dwResourceSize,
                                      static_cast<LPVOID>(m_pucResourceData)) != FALSE)
              {
                UINT uiSize = 0;
    
                // Get language information
                if(::VerQueryValue(static_cast<LPVOID>(m_pucResourceData),
                                   "\\VarFileInfo\\Translation",
                                   reinterpret_cast<LPVOID *>(&m_pLanguageInformation),
                                   &uiSize) == FALSE)
                  throw std::exception("Requested localized version information not available");
              }
              else
              {
                std::stringstream strsException;
                strsException << "Could not get version information (Windows error: " << ::GetLastError() << ")";
                throw std::exception(strsException.str().c_str());
              }
            }
            else
              throw std::bad_alloc();
          }
          else
          {
            std::stringstream strsException;
            strsException << "No version information found (Windows error: " << ::GetLastError() << ")";
            throw std::exception(strsException.str().c_str());
          }
        }
        else
          throw std::exception("Could not get application name");
      }
    
      ~CVersion() { delete [] m_pucResourceData; }
      std::string GetProductName() const { return GetValue("ProductName"); }
      std::string GetInternalName() const { return GetValue("InternalName"); }
      std::string GetProductVersion() const { return GetValue("ProductVersion"); }
      std::string GetSpecialBuild() const { return GetValue("SpecialBuild"); }
      std::string GetPrivateBuild() const { return GetValue("PrivateBuild"); }
      std::string GetCopyright() const { return GetValue("LegalCopyright"); }
      std::string GetTrademarks() const { return GetValue("LegalTrademarks"); }
      std::string GetComments() const { return GetValue("Comments"); }
      std::string GetCompanyName() const { return GetValue("CompanyName"); }
      std::string GetFileVersion() const { return GetValue("FileVersion"); }
      std::string GetFileDescription() const { return GetValue("FileDescrition"); }
    
    private:
      unsigned char     *m_pucResourceData;                   // Resource data
      Version::Language *m_pLanguageInformation;              // Language information
    
      std::string GetValue(const std::string &refcstrKey) const
      {
        if(m_pucResourceData)
        {
          UINT              uiSize   = 0;
          std::stringstream strsTemp;
          LPVOID            pvValue  = 0;
    
          // Build query string
          strsTemp << "\\StringFileInfo\\" << std::setw(4) << std::setfill('0') << std::hex
                   << m_pLanguageInformation->m_wLanguage << std::setw(4) << std::hex
                   << m_pLanguageInformation->m_wCodePage << "\\" << refcstrKey;
    
          if(::VerQueryValue(static_cast<LPVOID>(m_pucResourceData),
                             const_cast<LPTSTR>(strsTemp.str().c_str()),
                             static_cast<LPVOID *>(&pvValue),
                             &uiSize) != FALSE)
            return static_cast<LPTSTR>(pvValue);
        }
    
    
        return "";
      }
    };
    
    #endif
    Last edited by Andreas Masur; August 5th, 2004 at 10:24 AM.

  3. #3
    Join Date
    Jun 2004
    Location
    England
    Posts
    90

    Thumbs up

    Thank you
    You reply is very useful

    The most knowledgeable people are those who know that they know nothing.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    You are welcome...

  5. #5
    Join Date
    Jun 2004
    Location
    England
    Posts
    90
    strsException << "Could not get version information (Windows error: " << ::GetLastError() >> ")";

    I think there was a typing error there,

    strsException << "Could not get version information (Windows error: " << ::GetLastError() << ")";

    thanks
    The most knowledgeable people are those who know that they know nothing.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Quote Originally Posted by Tamerocyte
    I think there was a typing error there,
    Yep...sorry about that...

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    Quote Originally Posted by Tamerocyte
    Hi all,

    I have developed an application with MS VC++ 6.0
    I have set the version resource and want to know how to acquire this version info in my application so I can display it in a dialog. Is there an API for this, my search of msdn failed... any assistance will be much obliged

    thanks
    You could also take a look at this article: Get VersionInfo from resource file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured