Get VersionInfo from resource file

.

CVersionInfo is a tiny class, which wraps the stuff needed to get the version info from a resource file. It handles both, the fixed version information and the language dependent string version information of the resource file. It contains three CString members which hold the retrieved information:


    m_strFixedFileVersion:  the fixed file version info (language independent)
    m_strFixedProductVersion: the fixed product version info (language independent)
    m_strVersionInfo:   the desired version info string (language dependent)

To get the language dependent string version info you need to set the following parameters as follows:

strLangID:


strLangID is the language ID for which the version info is desired
040904E4 means e.g.:
04------ = SUBLANG_ENGLISH_USA
--09---- = LANG_ENGLISH
----04E4 = 1252 = Codepage for Windows:Multilingual

strInfoType:


strInfoType is the desired version entry, eg.:"ProductName", or "ProductVersion"

hModule:


hModule ist the Instance handle of the module for which the version info should be retrieved
with EXE's: Get hModule calling AfxGetInstanceHandle.
with DLL's: Get hModule calling ::GetModuleHandle ("DLLName"), where "DLLName" is the Name of the module

Important!

CVersionInfo requires to be linked with VERSION.LIB!

Here's the code:
 
// VersionInfo.h 
 
#ifndef VERSIONINFO_H
#define VERSIONINFO_H 
#include "stdafx.h"
class CVersionInfo
{
public:
	CString m_strVersionInfo;
	CString m_strFixedFileVersion;
	CString m_strFixedProductVersion;
	CVersionInfo ();
	~CVersionInfo ();
	CVersionInfo (HMODULE hModule, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
	CVersionInfo (LPTSTR szFilename, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
	void GetVersionInfo (LPTSTR szFilename, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
	void GetVersionInfo (HMODULE hModule, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
}; 

#endif // VERSIONINFO_H
 
// VersionInfo.cpp
 
//**********************************************************************************************
// CVersionInfo (c)1997 Roberto Rocco 
//----------------------------------------------------------------------------------------------
// CVersionInfo is a tiny class, which wraps the stuff needed to get the version info from a
// resource file.
// It handles both, the fixed version information and the language dependent string version
// information of the resource file.
//
// It contains three CString members which hold the retrieved information:
// m_strFixedFileVersion:  the fixed file version info (language independent)
// m_strFixedProductVersion: the fixed product version info (language independent)
// m_strVersionInfo:   the desired version info string (language dependent)
//
// CVersionInfo requires to be linked with VERSION.LIB!
//
//********************************************************************************************** 
#include "VersionInfo.h"
// Standard-Constructor. Does nothing particular
CVersionInfo::CVersionInfo ()
{
} 

// Standard-Destructor. Does nothing particular
CVersionInfo::~CVersionInfo ()
{
} 

// Constructor with hModule, strLangId and strInfoType as parameter
CVersionInfo::CVersionInfo (HMODULE hModule, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	GetVersionInfo (hModule, strLangID, strInfoType);
}
 
// Constructor with szFilename, strLangId and strInfoType as parameter
CVersionInfo::CVersionInfo (LPTSTR szFilename, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	GetVersionInfo (szFilename, strLangID, strInfoType);
}

void CVersionInfo::GetVersionInfo (HMODULE hModule, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	TCHAR szExeName[MAX_PATH];  
	if(hModule == NULL) 
		  return;  
	GetModuleFileName(hModule, szExeName, sizeof (szExeName)); 
	GetVersionInfo(szExeName, strLangID, strInfoType); 
} 

//**********************************************************************************************
// GetVersionInfo (requires VERSION.LIB!!!)
//----------------------------------------------------------------------------------------------
// Retrieves the desired version entry from the VERSIONINFO structure of the resource file.
//
// strLangID is the language ID for which the version info is desired
// 040904E4 means e.g.:
// 04------        = SUBLANG_ENGLISH_USA
// --09----        = LANG_ENGLISH
// ----04E4 = 1252 = Codepage for Windows:Multilingual
//
// strInfoType  is the desired version entry, eg.:"ProductName", or "ProductVersion"
//
// hModule ist the Instance handle of the module for which the version info should be retrieved
// with EXE's: Get hModule calling AfxGetInstanceHandle.
// with DLL's: Get hModule calling  ::GetModuleHandle ("DLLName"), where "DLLName" is the Name of the module
//
// the version information is stored in the member variables:
// m_strFixedFileVersion, m_strFixedProductVersion and m_strVersionInfo, where:
// m_strFixedFileVersion and m_strFixedProductVersion contain the fixed version info (language independent) and
// m_strVersionInfo contains the desired version info string (language dependent)
//
//**********************************************************************************************
void CVersionInfo::GetVersionInfo (LPTSTR szFilename, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	DWORD dwVerInfoSize;
	DWORD dwHnd;
	void* pBuffer; 
	VS_FIXEDFILEINFO *pFixedInfo; // pointer to fixed file info structure
	LPVOID  lpVersion;    // String pointer to 'version' text
	UINT    uVersionLen;   // Current length of full version string
	TCHAR szGetName[500]; 
	 dwVerInfoSize = GetFileVersionInfoSize(szFilename, &dwHnd); 
	 if (dwVerInfoSize) 
	{ 
		  pBuffer = malloc(dwVerInfoSize); 
		  if (pBuffer == NULL)
			   return; 
		  GetFileVersionInfo(szFilename, dwHnd, dwVerInfoSize, pBuffer); 
		  // get the fixed file info (language-independend) 
		  VerQueryValue(pBuffer,_T("\\"),(void**)&pFixedInfo,(UINT *)&uVersionLen);  
		  m_strFixedProductVersion.Format ("%u,%u,%u,%u", HIWORD (pFixedInfo->dwProductVersionMS),
			              LOWORD (pFixedInfo->dwProductVersionMS),
			              HIWORD (pFixedInfo->dwProductVersionLS),
			              LOWORD (pFixedInfo->dwProductVersionLS)); 
		  m_strFixedFileVersion.Format ("%u,%u,%u,%u",HIWORD (pFixedInfo->dwFileVersionMS),
			             LOWORD (pFixedInfo->dwFileVersionMS),
			             HIWORD (pFixedInfo->dwFileVersionLS),
			             LOWORD (pFixedInfo->dwFileVersionLS)); 
		  // get the string file info (language-dependend) 
		  if (strLangID != NULL || strInfoType != NULL)
		  {
			   lstrcpy(szGetName, "\\StringFileInfo\\");  
			   lstrcat (szGetName, strLangID);
			   lstrcat (szGetName, "\\");
			   lstrcat (szGetName, strInfoType);
			   // copy version info, if desired entry exists
			   if (VerQueryValue(pBuffer,szGetName,(void**)&lpVersion,(UINT *)&uVersionLen) != 0)
				    m_strVersionInfo = (LPTSTR)lpVersion;
		  } 
		  if (pBuffer != NULL)
			   free(pBuffer); 
	} 
} 



Comments

  • Doesn't work for a Service

    Posted by Legacy on 12/02/2002 12:00am

    Originally posted by: BaliDawg

    When I drop this code into my CComModule-based ATL Service, GetFileVersionInfoSize() always returns 0. I even tried running this code from an MFC-based test app and it still returns 0.

    I'm guessing the VS_VERSION_INFO structure is not making it into the Service build, because if I right click on the Service.exe file from windows explorer and select Properties, there is no Version tab.

    Any one know why?

    Reply
  • thanks much! works great 4 me!

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

    Originally posted by: Jim S

    Once again, if I woulda just looked here to start, I might have been able to even more time.

    Thanks for posting this!

    Reply
  • fatal error C1010

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

    Originally posted by: Ch. Bock

    I'm desperatly try to get this piece of code compile, I get always "fatal error C1010 unexpected end of file while looking for precompiled header directive
    Error executing cl.exe. What is wrong?

    Reply
  • There is no finer investment for a community than putting useful stuff

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

    Originally posted by: giulio corradi

    I had almost finished the implementation of a very similar class, but your proved much useful. Thanks a lot

    Reply
  • Use #pragma in header to include version.lib!

    Posted by Legacy on 01/21/2000 12:00am

    Originally posted by: Peter Kling

    Insert this pragma statement into the header file to ensure that it will always be included in the build!
    
    

    #pragma comment(lib, "version.lib") // We must have that lib!

    Reply
  • Version info. in binary files?

    Posted by Legacy on 06/09/1999 12:00am

    Originally posted by: Carsten Pedersen

    Does anyone know how to add version info. to a binary file i MS Windows. Is it at all possible to add version info. to a non-executable file, so that the OS can read the info. in the properties of the file?

    Regards
    Carsten Pedersen

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's no secret what keeps CIOs up at night. Mobile, cloud, data, security, and social have become the "five imperatives," the drivers of business progress, innovation, and competitive differentiation. Business leaders around the world want to hear how other companies are succeeding. How are they applying the latest technologies? How did they get started? What outcomes are they achieving? Read this online magazine for success stories from organizations like the NBA, Pfizer, and San Jose State University as they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds